Want more features on Pastebin? Sign Up, it's FREE!
Guest

Test

By: a guest on Oct 31st, 2012  |  syntax: Java  |  size: 3.06 KB  |  views: 13  |  expires: Never
download  |  raw  |  embed  |  report abuse  |  print
Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
  1. // Implemented as a solution to the question in: http://stackoverflow.com/questions/13005964/thread-pool-where-workers-are-both-producers-and-consumers
  2.  
  3. import java.util.concurrent.PriorityBlockingQueue;
  4. import java.util.concurrent.RejectedExecutionHandler;
  5. import java.util.concurrent.ThreadPoolExecutor;
  6. import java.util.concurrent.TimeUnit;
  7.  
  8. public class Test {
  9.  
  10.     public void startTest(long maxComputationTimeMS, long maxJobTimeMS, int maxBranchingFactor, int numOfThreads) {
  11.        
  12.         // print the session details
  13.         System.out.println("Starting a new session where:");
  14.         System.out.println("\tMaximum computation time is: " + maxComputationTimeMS + "ms.");
  15.         System.out.println("\tJobs will take N milliseconds to complete, where [0 <= N < " + maxJobTimeMS + "].");
  16.         System.out.println("\tJobs may spawn N new jobs, where [0 <= N < " + maxBranchingFactor +"].");
  17.         System.out.println("\tThe executor will use a pool of " + numOfThreads + " threads.");
  18.        
  19.         // a new work queue which orders tasks based on compareTo()
  20.         PriorityBlockingQueue<Runnable> queue = new PriorityBlockingQueue<Runnable>();
  21.        
  22.         // a new thread pool executor using the above queue with a custom handler
  23.         TestExecutor executor = new TestExecutor(numOfThreads, numOfThreads, 1000, TimeUnit.MILLISECONDS, queue, new CancelledJobHandler());
  24.        
  25.         // schedule a new comparable work unit for execution
  26.         executor.executeJob(new WorkUnit(maxJobTimeMS, maxBranchingFactor, executor));
  27.        
  28.         // print out a start of work message
  29.         System.out.println("Work started...");
  30.  
  31.         // wait for the work to complete then print the completion status
  32.         if(executor.awaitCompletion(maxComputationTimeMS, TimeUnit.MILLISECONDS)) {
  33.                 System.out.println("Timeout encountered, main thread exiting...");
  34.         }
  35.         else {
  36.                 System.out.println("No more jobs queued, main thread exiting...");     
  37.         }
  38.        
  39.         // shut the executor down
  40.         executor.shutdownNow();
  41.        
  42.         // await at most one second for the termination of the executor
  43.         try {
  44.                         executor.awaitTermination(1, TimeUnit.SECONDS);
  45.                 } catch (InterruptedException e) {
  46.                         e.printStackTrace();
  47.                 }
  48.        
  49.         // print whether or not the executor terminated in time  
  50.         if(executor.isShutdown()) {
  51.                 System.out.println("Executor shut down in a timely manner.");  
  52.         }
  53.         else {
  54.                 System.err.println("Executor failed to shut down in a timely manner.");
  55.         }
  56.     }
  57.    
  58.     // start the test with a maximum computation time of 10 seconds
  59.     public static void main(String argv[]) throws InterruptedException {
  60.         Test t = new Test();
  61.         t.startTest(5000, 1000, 4, 3);
  62.     }
  63.    
  64.     // a custom exception handler to be used if the executor is terminated prematurely
  65.     public class CancelledJobHandler implements RejectedExecutionHandler {
  66.                 public void rejectedExecution(Runnable r, ThreadPoolExecutor executor) {
  67.                         System.err.println("Job cancelled: " + r);
  68.                 }
  69.     }
  70. }
clone this paste RAW Paste Data