class Number{
private int num = 0 ;
private boolean flag = true;
//flag = true 可以加不能减
//flas = false 可以减不能加
public synchronized void add(){
if(flag == false){
try {
super.wait(); // 等待加法操作
} catch (InterruptedException e) {
e.printStackTrace();
}
}
this.num ++ ;
try {
Thread.sleep(100);
} catch (InterruptedException e) {
e.printStackTrace();
线程加减法(出错)
最新推荐文章于 2021-03-23 11:23:14 发布