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内的锁对象,求大神解释!!!!