一般来讲,我们对于“修改”的语句要上锁,对于“输出”的语句不上锁。
public class Test_XCTB implements Runnable{
Timer timer = new Timer();
public static void main(String[] args) {
// TODO Auto-generated method stub
Test_XCTB test = new Test_XCTB();
Thread t1 = new Thread(test);
Thread t2 = new Thread(test);
t1.setName("t1");
t2.setName("t2");
t1.start(); t2.start();
}
public void run() { //因为Test_XCTB实现了Runnable接口,所以要重写run方法
timer.add(Thread.currentThread().getName());
}
}
class Timer{
private static int num = 0;
public void add(String neme) { //或者public synchronized void add(String neme)
synchronized(this) {
num++;
try {
Thread.sleep(1);//休眠1毫秒
} catch (InterruptedException e) {}
System.out.println("This is the " + num + "th");
}
}
}
/*
* 输出结果为:This is the 2th;This is the 2th;两个num居然一样,与期望的1th和2th不一样!
* 原因在于在执行t1的时候,num++变成1;但遇到了sleep方法,所以t1停止,执行t2;此时num++变成2;然后在执行输出语句。
* 但其实不加sleep语句也会遇到t1被打断而出现一样的结果。
* 解决方法是在num语句前加上synchronized(this){}方法,表示将当前线程锁定,num也随之被锁定。
* 还可以简便的在public void add(String neme)方法处加上synchronize,变成public synchronize void add。
*/