daily pastebin goal
31%
SHARE
TWEET

Untitled

a guest Oct 16th, 2018 67 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. package com.isea.data;
  2.  
  3. import java.util.concurrent.locks.Condition;
  4. import java.util.concurrent.locks.Lock;
  5. import java.util.concurrent.locks.ReentrantLock;
  6.  
  7. /**
  8.  * 线程操作资源类
  9.  * 判断,干活,唤醒
  10.  * 虚假唤醒
  11.  *
  12.  * 生产者消费者问题,交替消费和生产,两个生产者,两个消费者
  13.  */
  14.  
  15. class ShareData{
  16.     private int data = 0;
  17.     Lock lock = new ReentrantLock();
  18.     Condition  condition = lock.newCondition();
  19.  
  20.     public  void produce() throws InterruptedException {
  21.         lock.lock();
  22.         try {
  23.             while (data != 0){
  24.                 condition.await();
  25.             }
  26.             data ++;
  27.             System.out.println(Thread.currentThread().getName() + "生产了,余量\t" + data);
  28.             condition.signalAll();
  29.  
  30.         } catch (Exception e) {
  31.             e.printStackTrace();
  32.         } finally {
  33.             lock.unlock();
  34.         }
  35.     }
  36.  
  37.     public synchronized void consumer() throws InterruptedException {
  38.  
  39.         lock.lock();
  40.         try {
  41.             while (data == 0){
  42.                 condition.await();
  43.             }
  44.             data --;
  45.             System.out.println(Thread.currentThread().getName() + "消费了,余量\t" + data);
  46.             condition.signalAll();
  47.          } catch (Exception e) {
  48.             e.printStackTrace();
  49.         } finally {
  50.             lock.unlock();
  51.         }
  52.  
  53.     }
  54. }
  55.  
  56. public class ProduceAndConsumer {
  57.     public static void main(String[] args) {
  58.         ShareData shareData = new ShareData();
  59.         new Thread(() -> {
  60.             for (int i = 0; i < 5; i++) {
  61.                 try {
  62.                     shareData.produce();
  63.                 } catch (InterruptedException e) {
  64.                     e.printStackTrace();
  65.                 }
  66.             }
  67.         },"Producer1").start();
  68.  
  69.         new Thread(() -> {
  70.             for (int i = 0; i < 5; i++) {
  71.                 try {
  72.                     shareData.consumer();
  73.                 } catch (InterruptedException e) {
  74.                     e.printStackTrace();
  75.                 }
  76.             }
  77.         },"Consumer1").start();
  78.  
  79.         new Thread(() -> {
  80.             for (int i = 0; i < 5; i++) {
  81.                 try {
  82.                     shareData.produce();
  83.                 } catch (InterruptedException e) {
  84.                     e.printStackTrace();
  85.                 }
  86.             }
  87.         },"Producer2").start();
  88.  
  89.         new Thread(() -> {
  90.             for (int i = 0; i < 5; i++) {
  91.                 try {
  92.                     shareData.consumer();
  93.                 } catch (InterruptedException e) {
  94.                     e.printStackTrace();
  95.                 }
  96.             }
  97.         },"Consumer2").start();
  98.     }
  99.  
  100.  
  101. }
  102. /*
  103. Producer1生产了,余量 1
  104. Consumer1消费了,余量 0
  105. Producer1生产了,余量 1
  106. Consumer1消费了,余量 0
  107. Producer1生产了,余量 1
  108. Consumer1消费了,余量 0
  109. Producer1生产了,余量 1
  110. Consumer1消费了,余量 0
  111. Producer1生产了,余量 1
  112. Consumer1消费了,余量 0
  113. Producer2生产了,余量 1
  114. Consumer2消费了,余量 0
  115. Producer2生产了,余量 1
  116. Consumer2消费了,余量 0
  117. Producer2生产了,余量 1
  118. Consumer2消费了,余量 0
  119. Producer2生产了,余量 1
  120. Consumer2消费了,余量 0
  121. Producer2生产了,余量 1
  122. Consumer2消费了,余量 0
  123. */
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top