public class Test {
public static void main(String[] args)throws Exception {
Test test = new Test();
MyService service = test.new MyService();
ThreadA threadA = test.new ThreadA(service);
threadA.setName("threadA");
ThreadB threadB = test.new ThreadB(service);
threadB.setName("threadB");
threadA.start();
//Thread.sleep(50);
threadB.start();
}
public class MyService{
private String lock = "123";
public void testMethod(){
try{
synchronized(lock){
System.out.println(Thread.currentThread().getName() + "begain " + System.currentTimeMillis());
lock="456";
Thread.sleep(2000);
System.out.println(Thread.currentThread().getName() + "end" +System.currentTimeMillis());
}
}catch (Exception e) {
e.printStackTrace();
}
}
}
public class ThreadA extends Thread{
private MyService service;
public ThreadA(MyService service) {
this.service = service;
}
@Override
public void run() {
service.testMethod();
}
}
public class ThreadB extends Thread{
private MyService service;
public ThreadB(MyService service) {
this.service = service;
}
@Override
public void run() {
service.testMethod();
}
}
}
問題是這樣的,我在看書時沒想明白,就是執行sleep(50)這句話後兩個線程就不同步了,去掉sleep(50)後線程同步,我想了一下這關系到線程什麼時候確定synchronized內的鎖對象,求大神解釋!!!!
你的鎖是一個String對象 lock,在synchronized代碼段裡面你把lock對象重新賦值了,在java中String有一個特別的特性,每次賦值其實是產生一個新的String對象。執行了sleep(50)後,導致裡threadA先運行了,並且是執行過了對lock字符串的賦值,threadB後運行,這個時候賦值過後的lock是一個新的對象,沒有鎖,所以threadB可以執行這個代碼段。你把lock="456";這行去掉,就算執行了sleep(50)兩個線程還是同步的。