public class Test5 {
public synchronized void f1(){
System.out.println(Thread.currentThread().getName() + " f1 start");
try {
Thread.sleep(10000);
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println(Thread.currentThread().getName()+" f1 end ");
}
public void f2(){
try {
Thread.sleep(5000);
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println(Thread.currentThread().getName()+" f2");
}
public static void main(String[] args) {
Test5 t = new Test5();
new Thread(()->t.f1()).start();
new Thread(()->t.f2()).start();
}
}
问1:执行f1的过程中,f2能否被执行呢?
可以。因为f2是非同步方法,执行不需要申请锁,f2的执行和f1的执行互不影响。