public class Solution {
public static void main(String[] args) {
Lock lock = new ReentrantLock();
Thread t1 = new Thread(()->{
try {
lock.lock();
System.out.println("线程一启动");
TimeUnit.SECONDS.sleep(Integer.MAX_VALUE);
System.out.println("线程一结束");
} catch (InterruptedException e) {
System.out.println("线程一中断");
} finally {
lock.unlock();
}
});
Thread t2 = new Thread(()->{
try {
lock.lock();
System.out.println("线程二启动");
TimeUnit.SECONDS.sleep(5);
System.out.println("线程二启动");
} catch (InterruptedException e) {
System.out.println("线程二中断");
} finally {
lock.unlock();
}
});
t1.start();
t2.start();
try {
TimeUnit.SECONDS.sleep(1);
} catch (InterruptedException e) {
e.printStackTrace();
}
t2.interrupt();
}
}
程序输出结果仅有【线程一启动】,线程二不会被执行
请教在线程 t1 中的 lock.lock() 究竟是锁的哪个对象?没看到程序里有显式声明对象
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…