import java.util.concurrent.locks.ReentrantLock;
public class ReentrantLockDemo01 implements Runnable {
private static ReentrantLock reentrantLock = new ReentrantLock(true);
@Override
public void run() {
while (true){
reentrantLock.lock();
try{
System.out.println(Thread.currentThread().getName()+" get lock");
Thread.sleep(1000);
}catch (InterruptedException e){
e.printStackTrace();
}finally {
reentrantLock.unlock();
}
}
}
public static void main(String[] args) {
ReentrantLockDemo01 reentrantLockDemo01 = new ReentrantLockDemo01();
Thread thread1 = new Thread(reentrantLockDemo01);
Thread thread2 = new Thread(reentrantLockDemo01);
thread1.start();
thread2.start();
}
}