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)两个线程还是同步的。