循环打印ABA问题
注意!! notify 虛擬鎖問題,后續補充?
package com.qey.learn;/*** @ClassName SwapPrint* @Description* @Author qianxl* @Date 2021-03-02 14:46* @Version 1.1**/ public class SwapPrint {public int flag =0;public synchronized void printA() {while (flag>=1) {try {this.wait();} catch (InterruptedException e) {e.printStackTrace();}}flag++;System.out.println("A");this.notifyAll();}public synchronized void printB() {while (flag<=0) {try {this.wait();} catch (InterruptedException e) {e.printStackTrace();}}flag--;System.out.println("B");this.notifyAll();}public static void main(String[] args) {SwapPrint swapPrint = new SwapPrint();Thread thread = new Thread(new Runnable() {@Overridepublic void run() {while(true){swapPrint.printA();}}});Thread thread1 = new Thread(new Runnable() {@Overridepublic void run() {while (true){swapPrint.printB();}}});thread1.start();thread.start();}}總結
- 上一篇: xp下添加linux启动,如何在wind
- 下一篇: linux 扩充db2表空间,如何扩充d