* 题目: 现在两个线程, 可以操作初始值为零的一个变量,
* 实现一个线程对该变量加 1, 一个线程对该变量减 1,
* 实现交替, 来 10 轮, 变量初始值为零.
看代码:
- package com.cxy.juc;
- class AirCondition{
- private int num = 0;
- public synchronized void increment() throws Exception{
- if(num !=0){
- this.wait();
- }
- num++;
- System.out.println(Thread.currentThread().getName()+"\t"+num);
- this.notifyAll();
- }
- public synchronized void decrement() throws Exception{
- if (num ==0){
- this.wait();
- }
- num--;
- System.out.println(Thread.currentThread().getName()+"\t"+num);
- this.notifyAll();
- }
- }
- /**
- * 多线程操作: 线程操作资源类,
- * 判断, 干活, 通知
- * 防止虚假唤醒
- */
- public class ProdConsumerDemo {
- public static void main(String[] args) throws Exception{
- AirCondition airCondition =new AirCondition();
- new Thread(() ->{
- for (int i = 0; i <10 ; i++) {
- try {
- airCondition.increment();
- } catch (Exception e) {
- e.printStackTrace();
- }
- }
- },"a").start();
- /*new Thread(() ->{
- for (int i = 0; i <10 ; i++) {
- try {
- airCondition.increment();
- } catch (Exception e) {
- e.printStackTrace();
- }
- }
- },"c").start();*/
- new Thread(() ->{
- for (int i = 0; i <10 ; i++) {
- try {
- airCondition.decrement();
- } catch (Exception e) {
- e.printStackTrace();
- }
- }
- },"b").start();
- /* new Thread(() ->{
- for (int i = 0; i <10 ; i++) {
- try {
- airCondition.decrement();
- } catch (Exception e) {
- e.printStackTrace();
- }
- }
- },"d").start();*/
- }
- }
执行结果是对的,
- a 1
- b 0
- a 1
- b 0
- a 1
- b 0
- a 1
- b 0
- a 1
- b 0
- a 1
那么线程再增多呢, 变成两个线程增加两个线程减少呢:
- package com.cxy.juc;
- class AirCondition{
- private int num = 0;
- public synchronized void increment() throws Exception{
- if(num !=0){
- this.wait();
- }
- num++;
- System.out.println(Thread.currentThread().getName()+"\t"+num);
- this.notifyAll();
- }
- public synchronized void decrement() throws Exception{
- if (num ==0){
- this.wait();
- }
- num--;
- System.out.println(Thread.currentThread().getName()+"\t"+num);
- this.notifyAll();
- }
- }
- /**
- * 多线程操作: 线程操作资源类,
- * 判断, 干活, 通知
- * 防止虚假唤醒
- */
- public class ProdConsumerDemo {
- public static void main(String[] args) throws Exception{
- AirCondition airCondition =new AirCondition();
- new Thread(() ->{
- for (int i = 0; i <10 ; i++) {
- try {
- airCondition.increment();
- } catch (Exception e) {
- e.printStackTrace();
- }
- }
- },"a").start();
- new Thread(() ->{
- for (int i = 0; i <10 ; i++) {
- try {
- airCondition.increment();
- } catch (Exception e) {
- e.printStackTrace();
- }
- }
- },"c").start();
- new Thread(() ->{
- for (int i = 0; i <10 ; i++) {
- try {
- airCondition.decrement();
- } catch (Exception e) {
- e.printStackTrace();
- }
- }
- },"b").start();
- new Thread(() ->{
- for (int i = 0; i <10 ; i++) {
- try {
- airCondition.decrement();
- } catch (Exception e) {
- e.printStackTrace();
- }
- }
- },"d").start();
- }
- }
看结果:
- b 0
- c 1
- d 0
- a 1
- d 0
- c 1
- b 0
- d -1
- d -2
- a -1
- b -2
- b -3
- a -2
那么出现 bug 了, 而且是 API 级别的 bug, 怎么办呢,
- synchronized (obj) {
- while (<condition does not hold>)
- obj.wait();
- ... // Perform action appropriate to condition
- a 1
- b 0
- c 1
- d 0
- a 1
- b 0
- c 1
- d 0
- a 1
- b 0
- c 1
- d 0
- a 1
- d 0
- c 1
- d 0
来源: http://www.bubuko.com/infodetail-3237691.html