public class TestSync implements Runnable{
int num = 100;
public static void main(String[] args){
TestSync syn = new TestSync();
Thread t = new Thread(syn);
t.start();
try{
Thread.sleep(1000);
} catch(InterruptedException e){
e.printStackTrace();
}
syn.num = 500;
System.out.println(syn.num);
}
public synchronized void run(){
num = 10000;
try{
Thread.sleep(5000);
} catch(InterruptedException e){
e.printStackTrace();
}
System.out.println("num:" + num);
}
}
来源:https://www.cnblogs.com/yxfyg/p/12421692.html