Package java.util.concurrent

Examples of java.util.concurrent.Semaphore


  public BlacklistLatch(final int count, final long timeout)
  {
    this.handlerTasks = new ArrayList<HandlerTask>(count);
    this.count = count;
    this.timeout = timeout;
    internalSemaphore = new Semaphore(count);
    internalSemaphore.drainPermits();
  }
View Full Code Here


    public ParallelJobQueue(final String name,
                           final InternalQueueConfiguration config,
                           final QueueServices services,
                           final Set<String> topics) {
        super(name, config, services, topics);
        this.available = new Semaphore(config.getMaxParallel(), true);
    }
View Full Code Here

      perfBlastPages = config.getJournalPerfBlastPages();

      if (config.getPageMaxConcurrentIO() != 1)
      {
         pageMaxConcurrentIO = new Semaphore(config.getPageMaxConcurrentIO());
      }
      else
      {
         pageMaxConcurrentIO = null;
      }
View Full Code Here

        Semaphore semaphore;
        int delay;

        public ConnectionStub(int delay, int expectedCalls) {
            this.semaphore = new Semaphore(1 - expectedCalls);
            this.delay = delay;
        }
View Full Code Here

    TSOServerConfig config;
   
    BookKeeperStateBuilder(TSOServerConfig config) {
        this.timestampOracle = new TimestampOracle();
        this.config = config;
        this.throttleReads = new Semaphore(PARALLEL_READS);
    }
View Full Code Here

  protected long nodeStartTimeStamp;
 
  public AbstractNode()
  {
    queue = new LinkedBlockingQueue<E>();
    pauseSemaphore = new Semaphore(1);
    inspector = new Inspector();
    inspector.start();
  }
View Full Code Here

                    ClientContextJME.getWorldManager().getRenderManager().setDesiredFrameRate(1);
                }
            });
        }

        final Semaphore waitForReady = new Semaphore(0);

        // Wait for the renderer to become ready
        rb.setBufferUpdater(new BufferUpdater() {

            public void init(RenderBuffer arg0) {
                logger.info("RENDERER IS READY !");
                waitForReady.release();

                // OWL issue #14: ignore repaints after the first to avoid
                // flickering on Windows. The first paint is necessary to
                // setup the canvas.  Once we get to this point, the canvas
                // is initialized, and we can ignore further repaints.
                canvas.setIgnoreRepaint(true);
            }
        });

        // issue 999: don't add the canvas until after the BufferUpdater is
        // registered, to make sure we don't miss the initialization call.  Also
        // force a repaint to be sure the initialization call happens eventually,
        // even on headless clients
        panel.add(canvas, BorderLayout.CENTER);
        canvas.repaint();

        try {
            waitForReady.acquire();
        } catch (InterruptedException ex) {
            Logger.getLogger(ViewManager.class.getName()).log(Level.SEVERE, null, ex);
        }

        createCameraEntity(ClientContextJME.getWorldManager());
View Full Code Here

     */
    @SuppressWarnings("unchecked")
    @Test
    public void testServiceExport() throws Exception {

        final Semaphore sema = new Semaphore(1);
        sema.acquire();
       
        String scope = "(objectClass=abc)";
    
        IMocksControl c = EasyMock.createNiceControl();
       
       
        BundleContext bctx = c.createMock(BundleContext.class);
       
        Bundle topMgrBundle = c.createMock(Bundle.class);
       
        RemoteServiceAdmin rsa = c.createMock(RemoteServiceAdmin.class);
        final ServiceReference rsaSref = c.createMock(ServiceReference.class);
        EndpointListener epl = c.createMock(EndpointListener.class);
        final ServiceReference eplSref = c.createMock(ServiceReference.class);
        EasyMock.expect(eplSref.getProperty(EasyMock.same(EndpointListener.ENDPOINT_LISTENER_SCOPE)))
            .andReturn(scope).anyTimes();
        EasyMock.expect(eplSref.getBundle()).andReturn(topMgrBundle).anyTimes();

        final ServiceReference sref = c.createMock(ServiceReference.class);
        EasyMock.expect(sref.getProperty(EasyMock.same(RemoteConstants.SERVICE_EXPORTED_INTERFACES)))
            .andReturn("*").anyTimes();
        Bundle srefBundle = c.createMock(Bundle.class);
        EasyMock.expect(sref.getBundle()).andReturn(srefBundle).anyTimes();
       
       
        EndpointDescription endpoint = c.createMock(EndpointDescription.class);
        {
            Map props = new HashMap();
            String[] objs = new String[1];
            objs[0] = "abc";
            props.put("objectClass", objs);
            EasyMock.expect(endpoint.getProperties()).andReturn(props).anyTimes();
        }

        ExportRegistration exportRegistration = c.createMock(ExportRegistration.class);
        ExportReference exportReference = c.createMock(ExportReference.class);

        EasyMock.expect(exportRegistration.getExportReference()).andReturn(exportReference).anyTimes();
        EasyMock.expect(exportReference.getExportedEndpoint()).andReturn(endpoint).anyTimes();
        {
            List ret = new ArrayList();
            ret.add(exportRegistration);
            EasyMock.expect(rsa.exportService(EasyMock.same(sref), (Map)EasyMock.anyObject())).andReturn(ret)
                .once();
        }

        epl.endpointAdded((EndpointDescription)EasyMock.anyObject(), (String)EasyMock.anyObject());
        EasyMock.expectLastCall().andAnswer(new IAnswer<Object>() {

            public Object answer() throws Throwable {
                System.out.println("Call made !!!");
                sema.release();
                return null;
            }
           
        }).once();

        {/* BCTX */

            bctx.addServiceListener((ServiceListener)EasyMock.anyObject(), (String)EasyMock.anyObject());
            EasyMock.expectLastCall().andAnswer(new IAnswer<Object>() {

                public Object answer() throws Throwable {
                    System.out.println("->   addServiceListener: "
                                       + EasyMock.getCurrentArguments()[1]);
                    ServiceListener sl = (ServiceListener)EasyMock.getCurrentArguments()[0];

                    if ("(objectClass=org.osgi.service.remoteserviceadmin.RemoteServiceAdmin)"
                        .equals(EasyMock.getCurrentArguments()[1])) {
                        ServiceEvent se = new ServiceEvent(ServiceEvent.REGISTERED, rsaSref);
                        sl.serviceChanged(se);
                    } else if ("(objectClass=org.osgi.service.remoteserviceadmin.EndpointListener)"
                        .equals(EasyMock.getCurrentArguments()[1])) {
                        ServiceEvent se = new ServiceEvent(ServiceEvent.REGISTERED, eplSref);
                        sl.serviceChanged(se);
                    }

                    return null;
                }
            }).anyTimes();

            bctx.addServiceListener((ServiceListener)EasyMock.anyObject());
            EasyMock.expectLastCall().andAnswer(new IAnswer<Object>() {

                public Object answer() throws Throwable {
                    System.out.println("->   addServiceListener ");

                    ServiceListener sl = (ServiceListener)EasyMock.getCurrentArguments()[0];

                    ServiceEvent se = new ServiceEvent(ServiceEvent.REGISTERED, sref);
                    sl.serviceChanged(se);
                    se = new ServiceEvent(ServiceEvent.REGISTERED, eplSref);
                    sl.serviceChanged(se);
                    se = new ServiceEvent(ServiceEvent.REGISTERED, rsaSref);
                    sl.serviceChanged(se);

                    return null;
                }
            }).anyTimes();

            EasyMock.expect(bctx.getService(EasyMock.same(rsaSref))).andReturn(rsa).anyTimes();
            EasyMock.expect(bctx.getService(EasyMock.same(eplSref))).andReturn(epl).atLeastOnce();

            {
                ServiceReference[] refs = new ServiceReference[1];
                refs[0] = eplSref;
                EasyMock
                    .expect(
                            bctx.getServiceReferences(EasyMock.same(EndpointListener.class.getName()),
                                                      EasyMock
                                                          .same("("
                                                                + EndpointListener.ENDPOINT_LISTENER_SCOPE
                                                                + "=*)"))).andReturn(refs).anyTimes();
            }
           
            EasyMock.expect(bctx.createFilter(EasyMock.same(scope))).andReturn(FrameworkUtil.createFilter(scope)).anyTimes();

        }

       
        c.replay();

        //        TopologyManager tm = new TopologyManager(bctx);
        //        tm.start();

        Activator a = new Activator();
        a.start(bctx);
       
        // Wait until the EndpointListener.endpointAdded call was made as the controlflow is asynchronous
        sema.tryAcquire(30, TimeUnit.SECONDS);
       
        c.verify();

    }
View Full Code Here

       
        IMocksControl c = EasyMock.createNiceControl();

        c.makeThreadSafe(true);
       
        final Semaphore sema = new Semaphore(0);
       
        BundleContext bc = c.createMock(BundleContext.class);
        ServiceRegistration sreg = c.createMock(ServiceRegistration.class);
        EasyMock.expect(bc.registerService((String)EasyMock.anyObject(), EasyMock.anyObject(), (Dictionary)EasyMock.anyObject())).andReturn(sreg).anyTimes();
        
       
        EndpointDescription epd = c.createMock(EndpointDescription.class);
        RemoteServiceAdmin rsa  = c.createMock(RemoteServiceAdmin.class);
        final ImportRegistration ireg = c.createMock(ImportRegistration.class);
        ImportReference iref = c.createMock(ImportReference.class);
       
        EasyMock.expect(rsa.importService(EasyMock.eq(epd))).andAnswer(new IAnswer<ImportRegistration>( ) {

            public ImportRegistration answer() throws Throwable {
                sema.release();
                return ireg;
            }
        }).once();
        EasyMock.expect(ireg.getImportReference()).andReturn(iref).anyTimes();
        EasyMock.expect(iref.getImportedEndpoint()).andReturn(epd).anyTimes();
        c.replay();
       
       
        RemoteServiceAdminList rsaList = new RemoteServiceAdminList(bc);
       
        TopologyManagerImport tm = new TopologyManagerImport(bc, rsaList);

        tm.start();
       
        // no RSa available yet so no import ... 
        tm.addImportableService("myFilter", epd);
       
        rsaList.add(rsa);
       
        tm.triggerExportImportForRemoteSericeAdmin(rsa);
       
        assertTrue(sema.tryAcquire(10, TimeUnit.SECONDS));
       
       
        tm.stop();
       
        c.verify();
View Full Code Here

    final DistributedFileSystem fs =
        (DistributedFileSystem)FileSystem.get(cluster.getURI(0), conf);
    DFSTestUtil.createFile(fs, new Path(TEST_FILE), TEST_FILE_LEN,
        (short)1, SEED);

    final Semaphore sem = new Semaphore(0);
    final List<LocatedBlock> locatedBlocks =
        cluster.getNameNode().getRpcServer().getBlockLocations(
            TEST_FILE, 0, TEST_FILE_LEN).getLocatedBlocks();
    final LocatedBlock lblock = locatedBlocks.get(0); // first block
    final byte[] buf = new byte[TEST_FILE_LEN];
    Runnable readerRunnable = new Runnable() {
      @Override
      public void run() {
        try {
          while (true) {
            BlockReader blockReader = null;
            try {
              blockReader = BlockReaderTestUtil.
                  getBlockReader(cluster, lblock, 0, TEST_FILE_LEN);
              sem.release();
              try {
                blockReader.readAll(buf, 0, TEST_FILE_LEN);
              } finally {
                sem.acquireUninterruptibly();
              }
            } catch (ClosedByInterruptException e) {
              LOG.info("got the expected ClosedByInterruptException", e);
              sem.release();
              break;
            } finally {
              if (blockReader != null) blockReader.close();
            }
            LOG.info("read another " + TEST_FILE_LEN + " bytes.");
          }
        } catch (Throwable t) {
          LOG.error("getBlockReader failure", t);
          testFailed.set(true);
          sem.release();
        }
      }
    };
    Thread thread = new Thread(readerRunnable);
    thread.start();

    // While the thread is reading, send it interrupts.
    // These should trigger a ClosedChannelException.
    while (thread.isAlive()) {
      sem.acquireUninterruptibly();
      thread.interrupt();
      sem.release();
    }
    Assert.assertFalse(testFailed.get());

    // We should be able to read from the file without
    // getting a ClosedChannelException.
View Full Code Here

TOP

Related Classes of java.util.concurrent.Semaphore

Copyright © 2018 www.massapicom. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.