1. synchronized线程锁
1.1. 使用
class data{
private Integer d=0;
public void d1(){
synchronized (d) {
d=1;
System.out.println(d);
}
try {
Thread.sleep(3000);
} catch (InterruptedException e) {
throw new RuntimeException(e);
}
}
public void d2() {
d=2;
System.out.println(d);
}
}
public class demo5 {
public static void main(String[] args) {
data d=new data();
new Thread(()->{
d.d1();
},"a").start();
new Thread(()->{
d.d2();
},"b").start();
}
}
原创2022/7/27大约 5 分钟