Advertisement
Guest User

Untitled

a guest
Feb 18th, 2020
139
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.25 KB | None | 0 0
  1. import java.util.concurrent.locks.ReentrantLock;
  2. import java.util.concurrent.locks.Condition;
  3.  
  4. class ConditionLock {
  5.   public static void main(String[] args) {
  6.     Store1 store = new Store1();
  7.     new Thread(new Producer1(store)).start();
  8.     new Thread(new Consumer1(store)).start();
  9.     new Thread(new Consumer1(store)).start();
  10.     new Thread(new Consumer1(store)).start();
  11.   }
  12. }
  13.  
  14. class Store1 {
  15.   private int product = 0;
  16.   private ReentrantLock locker;
  17.   private Condition conditionPut, conditionGet;
  18.   private boolean stateSales = true;
  19.  
  20.   Store1() {
  21.     locker = new ReentrantLock();
  22.     conditionPut = locker.newCondition();
  23.     conditionGet = locker.newCondition();
  24.   }
  25.  
  26.   public void put() {
  27.     try {
  28.       locker.lock();
  29.       while(product >= 3) {
  30.         if(!stateSales) return;
  31.         System.out.println("put...");
  32.         conditionPut.await();
  33.       }
  34.       product++;
  35.       System.out.println("put (1) - count " + product);
  36.       conditionGet.signalAll();
  37.     } catch (InterruptedException e) {
  38.       e.printStackTrace();
  39.     } finally {
  40.       locker.unlock();
  41.     }
  42.   }
  43.  
  44.   public void closeSales() {
  45.     locker.lock();
  46.     try {
  47.       stateSales = false;
  48.       conditionGet.signalAll();
  49.       conditionPut.signalAll();
  50.     } catch (Exception e) {
  51.       e.printStackTrace();
  52.     } finally {
  53.       locker.unlock();
  54.     }
  55.    
  56.   }
  57.  
  58.   public void get() {
  59.     try {
  60.       locker.lock();
  61.       while(product < 1) {
  62.         if(!stateSales) return;
  63.         System.out.println("get...");
  64.         conditionGet.await();
  65.       }
  66.       product--;
  67.       System.out.println("get (1) - count " + product);
  68.       conditionPut.signalAll();
  69.     } catch (InterruptedException e) {
  70.       e.printStackTrace();
  71.     } finally {
  72.       locker.unlock();
  73.     }
  74.   }
  75. }
  76.  
  77.  
  78. class Producer1 implements Runnable{
  79.  
  80.   Store1 store;
  81.   Producer1(Store1 store){
  82.      this.store=store;
  83.   }
  84.   public void run(){
  85.       for (int i = 1; i < 6; i++) {
  86.           store.put();
  87.       }
  88.       store.closeSales();
  89.   }
  90. }
  91.  
  92. class Consumer1 implements Runnable{
  93.    
  94.    Store1 store;
  95.   Consumer1(Store1 store){
  96.      this.store=store;
  97.   }
  98.   public void run(){
  99.       for (int i = 1; i < 6; i++) {
  100.           store.get();
  101.       }
  102.   }
  103. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement