Advertisement
Guest User

TestParallelism

a guest
May 22nd, 2012
97
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.88 KB | None | 0 0
  1. package jsr166y;
  2.  
  3. import java.util.concurrent.atomic.AtomicBoolean;
  4. import java.util.concurrent.atomic.AtomicInteger;
  5.  
  6. import org.junit.Test;
  7.  
  8. import junit.framework.Assert;
  9.  
  10. /**
  11.  * @author Quartet FS
  12.  */
  13. @SuppressWarnings("serial")
  14. public class TestParallelism {
  15.  
  16.     @Test
  17.     public void testWorkersAllocation() throws Exception {
  18.         final ForkJoinPool pool = new ForkJoinPool();
  19.         final AtomicBoolean success = new AtomicBoolean(false);
  20.         final AtomicInteger running = new AtomicInteger(0);
  21.  
  22.         // Run our test in the pool.
  23.         // We fork some work to do and make sure
  24.         // that we reach the expected parallelism level.
  25.         final int numIterations = 1 << 15;
  26.         pool.invoke(new RecursiveAction() {
  27.             @Override
  28.             protected void compute() {
  29.                 for (int i = 0; i < numIterations; ++i) {
  30.                     new RecursiveAction() {
  31.                         @Override
  32.                         protected void compute() {
  33.                             // No work if we have already achieved
  34.                             // great success
  35.                             if (success.get())
  36.                                 return;
  37.                            
  38.                             // Register our worker and do some work.
  39.                             final int numWorkers = running.incrementAndGet();
  40.                             try {
  41.                                 // Stop if we got enough workers
  42.                                 if (numWorkers >= pool.getParallelism()) {
  43.                                     success.set(true);
  44.                                     return;
  45.                                 }
  46.                                
  47.                                 // Burn some CPU.
  48.                                 doSomething();
  49.                             } finally {
  50.                                 running.decrementAndGet();
  51.                             }
  52.                         }
  53.                     }.fork();
  54.                 }
  55.                
  56.                 // Quiesce the pool before returning
  57.                 pool.helpQuiescePool(((ForkJoinWorkerThread)Thread.currentThread()).workQueue);
  58.             }
  59.         });
  60.  
  61.         // Make sure we were successful
  62.         Assert.assertTrue("Did not have enough workers allocated", success.get());
  63.     }
  64.    
  65.     /**
  66.      * Do something that will burn some CPU.
  67.      */
  68.     protected void doSomething() {
  69.         final long start = System.nanoTime();
  70.         do {} while ((System.nanoTime() - start) < 100L * 1000L * 1000L);
  71.     }
  72.    
  73.    
  74. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement