Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
  package eu.toolchain.async.perftests;
  
  
 
 
 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();
 
     public static class Tiny implements TestCase {
         @Override
         public void test() throws Exception {
             ListeningExecutorService service = MoreExecutors.listeningDecorator(Executors
                     .newFixedThreadPool());
 
             final AsyncFramework async = TinyAsync.builder().executor(service).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(10, .);
 
             if (sum.get() != )
                 throw new IllegalStateException(String.format(
                         "did not properly collect all values: expected %d, but was %d"sum.get()));
 
             service.shutdown();
         }
     }
 
     public static class Guava implements TestCase {
         @Override
         public void test() throws Exception {
             ListeningExecutorService service = MoreExecutors.listeningDecorator(Executors
                     .newFixedThreadPool());
 
             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 = service.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(10, .);
            if (sum.get() != )
                throw new IllegalStateException(String.format(
                        "did not properly collect all values: expected %d, but was %d"sum.get()));
            service.shutdown();
        }
    }
New to GrepCode? Check out our FAQ X