package com.company;
import java.util.concurrent.locks.ReentrantLock;
//测试Lock锁
public class TestLock implements Runnable{
public static void main(String[] args) {
TestLock testLock = new TestLock();
new Thread(testLock,"A").start();
new Thread(testLock,"B").start();
new Thread(testLock,"C").start();
}
int ticketNums = 10;
private final ReentrantLock lock= new ReentrantLock();
@Override
public void run() {
while(true){
lock.lock();
if(ticketNums>0){
try {
Thread.sleep(1000);
}catch (InterruptedException e){
e.printStackTrace();
}
System.out.println(ticketNums--);
}else{
break;
}
}
lock.unlock();
}
}