700字范文,内容丰富有趣,生活中的好帮手!
700字范文 > 生产者消费者1.0(wait notify)

生产者消费者1.0(wait notify)

时间:2023-08-28 23:01:09

相关推荐

生产者消费者1.0(wait notify)

/*** 描述: 生产者消费者 1.0 wait notify 版* wait notify** Thread1 对 number 加 1* Thread2 对 number 减 1* @author 小纸人* @create -07-31 20:30*/class ShareData1 {private int number = 0;public synchronized void increment() throws Exception {//1. 判断while (number != 0) {//等待不生产wait();}//2. 干活number++;System.out.println(Thread.currentThread().getName() + "\t" + number );//3. 通知唤醒notifyAll();}public synchronized void decrement() throws Exception {//1. 判断while (number == 0) {//等待不生产wait();}//2. 干活number--;System.out.println(Thread.currentThread().getName() + "\t" + number );//3. 通知唤醒notifyAll();}}public class Version1_0 {public static void main(String[] args){ShareData1 shareData1 = new ShareData1();new Thread(() -> {for (int i = 0; i < 5; i++) {try {shareData1.increment();} catch (Exception e) {e.printStackTrace();}}},"AAA").start();new Thread(() -> {for (int i = 0; i < 5; i++) {try {shareData1.decrement();} catch (Exception e) {e.printStackTrace();}}},"BBB").start();}}

本内容不代表本网观点和政治立场,如有侵犯你的权益请联系我们处理。
网友评论
网友评论仅供其表达个人看法,并不表明网站立场。