海康笔试问答题
代码如下,为什么是输出 call1 call2 call3。多次运行都是123,难道不应该是231或者123吗public class ThreadLock {
public static void main(String[] args) {
ThreadLock test=new ThreadLock();
Runnable runnable=new Runnable() {
@Override
public void run() {
test.method2();
test.method3();
}
};
Thread thread1=new Thread(runnable);
thread1.start();
test.method1();
}
public synchronized void method1(){
try {
Thread.sleep(10000);
System.out.println("call method1");
}catch (Exception e){
e.printStackTrace();
}
}
public synchronized void method2(){
System.out.println("call method2");
}
public synchronized void method3(){
System.out.println("call method3");
}
}
#海康威视##笔试题目#