Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
  package eu.toolchain.perftests.jmh;
  
  
 
 
 
 public class ManyListeners {
     private static final int SIZE = 10;
     private static final int CALLBACK_COUNT = 1000;
     private static final int EXPECTED_SUM = (( * ( - 1)) / 2) * ;
 
     private static int THREAD_COUNT = Runtime.getRuntime().availableProcessors();
 
     @Benchmark
     public void tiny() throws Exception {
         final ExecutorService executor = Executors.newFixedThreadPool();
         final AsyncFramework async = TinyAsync.builder().executor(executor).build();
 
         final AtomicInteger sum = new AtomicInteger();
         final CountDownLatch latch = new CountDownLatch(1);
         final CountDownLatch tasks = new CountDownLatch( * );
 
         final FutureResolved<Integercallback = new FutureResolved<Integer>() {
             @Override
             public void resolved(Integer result) {
                 sum.addAndGet(result);
                 tasks.countDown();
             }
         };
 
         for (int i = 0; i < i++) {
             final int current = i;
 
             final AsyncFuture<Integerfuture = async.call(new Callable<Integer>() {
                 @Override
                 public Integer call() throws Exception {
                     latch.await();
                     return current;
                 }
             });
 
             for (int c = 0; c < c++)
                 future.on(callback);
         }
 
         latch.countDown();
         tasks.await(1, .);
 
         if (sum.get() != )
             throw new IllegalStateException(String.format(
                     "did not properly collect all values: expected %d, but was %d"sum.get()));
 
         executor.shutdown();
     }
 
     @Benchmark
     public void guava() throws Exception {
         final ExecutorService executor = Executors.newFixedThreadPool();
         final ListeningExecutorService listeningExecutor = MoreExecutors.listeningDecorator(executor);
 
         final AtomicInteger sum = new AtomicInteger();
         final CountDownLatch latch = new CountDownLatch(1);
         final CountDownLatch tasks = new CountDownLatch( * );
 
         final FutureCallback<Integercallback = new FutureCallback<Integer>() {
             @Override
             public void onSuccess(Integer result) {
                 sum.addAndGet(result);
                 tasks.countDown();
             }
 
             @Override
             public void onFailure(Throwable t) {
             }
         };
 
         for (int i = 0; i < i++) {
             final int current = i;
 
             final ListenableFuture<Integerfuture = listeningExecutor.submit(new Callable<Integer>() {
                 @Override
                 public Integer call() throws Exception {
                     latch.await();
                    return current;
                }
            });
            for (int c = 0; c < c++) {
                Futures.addCallback(futurecallback);
            }
        }
        latch.countDown();
        tasks.await(1, .);
        if (sum.get() != )
            throw new IllegalStateException(String.format(
                    "did not properly collect all values: expected %d, but was %d"sum.get()));
        listeningExecutor.shutdown();
    }
New to GrepCode? Check out our FAQ X