创建对象
Lock l = new ReentrantLock();
Lock是ReentrantLock接口的实现类
l.lock();
if(ticket > 0){
try{
Thread.sleep(10);
System.out.println(Thread.currentThread().getName() + "-->正在卖出第" + ticket-- +"张票");
} catch (InterruptedException e) {
e.printStackTrace();
}finally {
l.unlock();
}
上锁
l.lock();
解锁
l.unlock();