04 ReentrantLock
public class T {
Lock lock = new ReentrantLock();
private synchronized void m1() {
System.out.println("m1 start");
}
private void m2() {
lock.lock(); // 等同于 synchronized
try {
System.out.println("m2 start");
} finally {
lock.unlock();
}
}
public static void main(String[] args) {
T t = new T();
new Thread(t::m1, "t1").start();
new Thread(t::m2, "t2").start();
}
}小结
最后更新于