Advertisement
Guest User

Untitled

a guest
Jul 21st, 2017
52
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.80 KB | None | 0 0
  1. import java.util.concurrent.locks.Condition;
  2. import java.util.concurrent.locks.Lock;
  3. import java.util.concurrent.locks.ReentrantLock;
  4.  
  5.  
  6. public class Task extends Thread{
  7.    
  8.         private int result;
  9.         private final Lock computeLock = new ReentrantLock();
  10.         private boolean computed = false;
  11.        
  12.         private final Condition canGet = computeLock.newCondition();
  13.        
  14.         private int compute() {
  15.             int val;
  16.            
  17.             val = 543;
  18.             return val;
  19.         }
  20.        
  21.         public int getResult() {
  22.            
  23.             computeLock.lock();
  24.            
  25.             try {
  26.                
  27.                 while (!computed) {
  28.                     try {
  29.                         canGet.await();
  30.                     } catch (InterruptedException e) {
  31.                         // TODO Auto-generated catch block
  32.                         e.printStackTrace();
  33.                     }
  34.                 }
  35.                
  36.                 return result;
  37.             } finally {
  38.                 computeLock.unlock();
  39.             }
  40.         }
  41.        
  42.         public void run() {
  43.             computeLock.lock();
  44.            
  45.             try {
  46.            
  47.                 result = compute();
  48.                 computed = true;
  49.                 canGet.signalAll();
  50.             } finally {
  51.                 computeLock.unlock();
  52.             }
  53.         }
  54.  
  55. }
  56.  
  57.  
  58. import java.util.concurrent.ExecutorService;
  59. import java.util.concurrent.Executors;
  60. import java.util.concurrent.TimeUnit;
  61.  
  62.  
  63. public class TestMe {
  64.  
  65.     /**
  66.      * @param args
  67.      */
  68.     public static void main(String[] args) {
  69.        
  70.         while (true) {
  71.         Task task1 = new Task();
  72.         Task task2 = new Task();
  73.        
  74.         ExecutorService threadExecutor = Executors.newCachedThreadPool();
  75.        
  76.         threadExecutor.execute(task1);
  77.         threadExecutor.execute(task2);
  78.        
  79.        
  80.         threadExecutor.shutdown();
  81.        
  82.  
  83.         System.out.println("result = " + (task1.getResult() + task2.getResult()));
  84.  
  85.        
  86.         try {
  87.             threadExecutor.awaitTermination(1, TimeUnit.DAYS);
  88.         } catch (InterruptedException ex) {
  89.             System.err.println("Something bad happened: " + ex.getMessage());
  90.         }
  91.        
  92.        
  93.        
  94.         }
  95.     }
  96.  
  97. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement