Package java.util.concurrent

Examples of java.util.concurrent.CyclicBarrier


  }
 
  @Test(timeout=20000)                                                        
  public void testConcurrentAddApplication()                                 
      throws IOException, InterruptedException, BrokenBarrierException {      
    final CyclicBarrier startBarrier = new CyclicBarrier(2);                  
    final CyclicBarrier endBarrier = new CyclicBarrier(2);                    
                                                                              
    // this token uses barriers to block during renew                         
    final Credentials creds1 = new Credentials();                             
    final Token<?> token1 = mock(Token.class);                                
    creds1.addToken(new Text("token"), token1);                               
    doReturn(true).when(token1).isManaged();                                  
    doAnswer(new Answer<Long>() {                                             
      public Long answer(InvocationOnMock invocation)                         
          throws InterruptedException, BrokenBarrierException {
        startBarrier.await();                                                 
        endBarrier.await();                                                   
        return Long.MAX_VALUE;                                                
      }}).when(token1).renew(any(Configuration.class));                       
                                                                              
    // this dummy token fakes renewing                                        
    final Credentials creds2 = new Credentials();                             
    final Token<?> token2 = mock(Token.class);                                
    creds2.addToken(new Text("token"), token2);                               
    doReturn(true).when(token2).isManaged();                                  
    doReturn(Long.MAX_VALUE).when(token2).renew(any(Configuration.class));    
                                                                              
    // fire up the renewer                                                    
    final DelegationTokenRenewer dtr =
        createNewDelegationTokenRenewer(conf, counter);          
    RMContext mockContext = mock(RMContext.class);                            
    ClientRMService mockClientRMService = mock(ClientRMService.class);        
    when(mockContext.getClientRMService()).thenReturn(mockClientRMService);   
    InetSocketAddress sockAddr =                                              
        InetSocketAddress.createUnresolved("localhost", 1234);                
    when(mockClientRMService.getBindAddress()).thenReturn(sockAddr);          
    dtr.setRMContext(mockContext)
    when(mockContext.getDelegationTokenRenewer()).thenReturn(dtr);
    dtr.init(conf);
    dtr.start();                                                                          
    // submit a job that blocks during renewal                                
    Thread submitThread = new Thread() {                                      
      @Override                                                               
      public void run() {
        dtr.addApplicationAsync(mock(ApplicationId.class), creds1, false);
      }                                                                       
    };                                                                        
    submitThread.start();                                                     
                                                                              
    // wait till 1st submit blocks, then submit another
    startBarrier.await();                          
    dtr.addApplicationAsync(mock(ApplicationId.class), creds2, false);
    // signal 1st to complete                                                 
    endBarrier.await();                                                       
    submitThread.join();
  }
View Full Code Here


    client.registerApplicationMaster(request);

    // grab the scheduler lock from another thread
    // and verify an allocate call in this thread doesn't block on it
    final CapacityScheduler cs = (CapacityScheduler) rm.getResourceScheduler();
    final CyclicBarrier barrier = new CyclicBarrier(2);
    Thread otherThread = new Thread(new Runnable() {
      @Override
      public void run() {
        synchronized(cs) {
          try {
            barrier.await();
            barrier.await();
          } catch (InterruptedException e) {
            e.printStackTrace();
          } catch (BrokenBarrierException e) {
            e.printStackTrace();
          }
        }
      }
    });
    otherThread.start();
    barrier.await();
    AllocateRequest allocateRequest =
        AllocateRequest.newInstance(0, 0.0f, null, null, null);
    client.allocate(allocateRequest);
    barrier.await();
    otherThread.join();

    rm.stop();
  }
View Full Code Here

      try {
         // Using a cyclic barrier, initialised with the number of keys to load,
         // in order to load all keys asynchronously and when the last one completes,
         // callback to the CompletionListener (via a barrier action).
         final CyclicBarrier barrier = new CyclicBarrier(keysToLoad.size(), new Runnable() {
            @Override
            public void run() {
               if (log.isTraceEnabled())
                  log.tracef("Keys %s loaded, notify listener on completion", keysToLoad);

               listener.onCompletion();
            }
         });
         FutureListener<V> futureListener = new FutureListener<V>() {
            @Override
            public void futureDone(Future<V> future) {
               try {
                  if (log.isTraceEnabled())
                     log.tracef("Key loaded, wait for the rest of keys to load");

                  barrier.await(30, TimeUnit.SECONDS);
               } catch (InterruptedException e) {
                  Thread.currentThread().interrupt();
               } catch (BrokenBarrierException e) {
                  setListenerException(listener, e);
               } catch (TimeoutException e) {
View Full Code Here

        KieSession ksession = createKnowledgeSession(kbase, conf);

        final BlockingQueue<TimedRuleExecution> queue = new LinkedBlockingQueue<TimedRuleExecution>();
        ((StatefulKnowledgeSessionImpl)ksession).session.setTimedExecutionsQueue(queue);

        final CyclicBarrier barrier = new CyclicBarrier(2);
        final AtomicBoolean run = new AtomicBoolean(true);

        Thread t = new Thread(new Runnable() {
            public void run() {
                try {
                    while (run.get()) {
                        queue.take().evauateAndFireRule();
                        try {
                            barrier.await();
                        } catch (BrokenBarrierException e) {
                            throw new RuntimeException(e);
                        }
                    }
                } catch (InterruptedException e) {
                    throw new RuntimeException(e);
                }
            }
        });
        t.setDaemon(true);
        t.start();

        List list = new ArrayList();

        PseudoClockScheduler timeService = ( PseudoClockScheduler ) ksession.<SessionClock>getSessionClock();
        timeService.advanceTime( new Date().getTime(), TimeUnit.MILLISECONDS );

        ksession.setGlobal( "list", list );

        ksession.fireAllRules();
        assertEquals( 0, list.size() );

        timeService.advanceTime(35, TimeUnit.SECONDS);
        barrier.await();
        barrier.reset();
        assertEquals( 1, list.size() );

        timeService.advanceTime(10, TimeUnit.SECONDS);
        barrier.await();
        barrier.reset();
        assertEquals( 2, list.size() );

        timeService.advanceTime(10, TimeUnit.SECONDS);
        barrier.await();
        barrier.reset();
        assertEquals( 3, list.size() );

        run.set(false);
        barrier.reset();
    }
View Full Code Here

  }

 
  public void ignoreTestConcurrentAccess() throws Exception {
    int threadCount = 100;
    CyclicBarrier cyclicBarrier = new CyclicBarrier(threadCount + 1);
    for (int i = 0; i < threadCount; i++) {
      new AccessThread(cyclicBarrier).start();
    }
    cyclicBarrier.await();
    cyclicBarrier.await();

  }
View Full Code Here

                    .maxConnectionPoolSize(128)
                    .nioPoolSize(clients)
                    .workerPoolSize(clients * 4).create();

            // let all the clients fully init before starting to send messages
            final CyclicBarrier barrier = new CyclicBarrier(clients);

            final List<Thread> threads = IntStream.range(0, clients).mapToObj(t -> new Thread(() -> {
                try {
                    final CountDownLatch latch = new CountDownLatch(requests);

                    final Client client = cluster.connect();
                    client.init();

                    barrier.await();
                    final long start = System.nanoTime();

                    System.out.println("Executing at [" + t + "]:" + start);

                    IntStream.range(0, requests).forEach(i -> {
View Full Code Here

        List<String> results = new ArrayList<String>();
        ksession.setGlobal("results", results);

        EPManipulator4[] epManipulators = new EPManipulator4[9];
        CyclicBarrier barrier = new CyclicBarrier(9, new SegmentChecker(epManipulators));
        for (int i = 0; i < 9; i++) {
            epManipulators[i] = new EPManipulator4(ksession, i+1, barrier);
        }

        new Thread () {
View Full Code Here

        KieSession ksession = createKnowledgeSession(kbase, conf);

        final BlockingQueue<TimedRuleExecution> queue = new LinkedBlockingQueue<TimedRuleExecution>();
        ((StatefulKnowledgeSessionImpl)ksession).session.setTimedExecutionsQueue(queue);

        final CyclicBarrier barrier = new CyclicBarrier(2);
        final AtomicBoolean run = new AtomicBoolean(true);

        Thread t = new Thread(new Runnable() {
            public void run() {
                try {
                    while (run.get()) {
                        queue.take().evauateAndFireRule();
                        try {
                            barrier.await();
                        } catch (BrokenBarrierException e) {
                            throw new RuntimeException(e);
                        }
                    }
                } catch (InterruptedException e) {
                    throw new RuntimeException(e);
                }
            }
        });
        t.setDaemon(true);
        t.start();

        List list = new ArrayList();

        PseudoClockScheduler timeService = ( PseudoClockScheduler ) ksession.<SessionClock>getSessionClock();
        timeService.advanceTime( new Date().getTime(), TimeUnit.MILLISECONDS );

        ksession.setGlobal( "list", list );

        ksession.fireAllRules();
        assertEquals( 0, list.size() );

        timeService.advanceTime(35, TimeUnit.SECONDS);
        barrier.await();
        barrier.reset();
        assertEquals( 1, list.size() );

        timeService.advanceTime(10, TimeUnit.SECONDS);
        barrier.await();
        barrier.reset();
        assertEquals( 2, list.size() );

        timeService.advanceTime(10, TimeUnit.SECONDS);
        barrier.await();
        barrier.reset();
        assertEquals( 3, list.size() );

        run.set(false);
        barrier.reset();
    }
View Full Code Here

        final int numServices = 5 ;
        final String category = "category" ;
        final String name = "name" ;
       
        final LocalRegistryInterceptor interceptor = new LocalRegistryInterceptor() ;
        final RegistryConcurrencyInterceptor concurrency = new RegistryConcurrencyInterceptor(new CyclicBarrier(numServices)) ;
        final MockRegistry registry = new MockRegistry() ;
        concurrency.setRegistry(registry) ;
        interceptor.setRegistry(concurrency) ;
       
        final String[] names = new String[numServices] ;
        for (int count = 0 ; count < numServices ; count++)
        {
            names[count] = name + count ;
            interceptor.registerEPR(category, names[count], "description", new EPR(), "EPR description") ;
        }
        final int numThreads = numServices*2 ;
       
        final CyclicBarrier barrier = new CyclicBarrier(numThreads) ;
        final ConcurrencyTest[] tests = new ConcurrencyTest[numThreads] ;
        for(int count = 0 ; count < numThreads ; count++)
        {
            tests[count] = new ConcurrencyTest(barrier, interceptor, category, names[count%numServices]) ;
            tests[count].start() ;
View Full Code Here

        final int numServices = 5 ;
        final String category = "category" ;
        final String name = "name" ;
       
        final CachingRegistryInterceptor interceptor = new CachingRegistryInterceptor() ;
        final RegistryConcurrencyInterceptor concurrency = new RegistryConcurrencyInterceptor(new CyclicBarrier(numServices)) ;
        final MockRegistry registry = new MockRegistry() ;
        concurrency.setRegistry(registry) ;
        interceptor.setRegistry(concurrency) ;
       
        final String[] names = new String[numServices] ;
        for (int count = 0 ; count < numServices ; count++)
        {
            names[count] = name + count ;
            registry.registerEPR(category, names[count], "description", new EPR(), "EPR description") ;
        }
        final int numThreads = numServices*2 ;
       
        final CyclicBarrier barrier = new CyclicBarrier(numThreads) ;
        final ConcurrencyTest[] tests = new ConcurrencyTest[numThreads] ;
        for(int count = 0 ; count < numThreads ; count++)
        {
            tests[count] = new ConcurrencyTest(barrier, interceptor, category, names[count%numServices]) ;
            tests[count].start() ;
View Full Code Here

TOP

Related Classes of java.util.concurrent.CyclicBarrier

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.