ALL > Computer and Education > courses > university courses > undergraduate courses > Operating System > zstu-(2021-2022)-1 > student homework directories > 2019327100017朱奕名 >
2019327100017朱奕名homework5 Version 0
👤 Author: by 1114774057qqcom 2021-12-30 04:47:19
Just when two datas are modified at the same time.

public class Sum implements Runnable{
static int sum=5;
boolean flag=true;
public Sum(boolean flag) {
super();
this.flag=flag;
}
public synchronized void run() {
while (-30<sum&&sum<30) {
if(flag) {
add();
}else {
sub();
}
}
}
public synchronized void add() {
sum=sum+4;
System.out.println(Thread.currentThread().getName()+"加后:"+sum);
}
public synchronized void sub() {
sum=sum-5;
System.out.println(Thread.currentThread().getName()+"减后:"+sum);
}
}

Please login to reply. Login

Reversion History

Loading...
No reversions found.