package com.nbuf.last;
import java.util.concurrent.locks.ReentrantLock;
public class TestLock {
public static void main(String[] args) {
Ticket windows=new Ticket();
Thread thread=new Thread(windows,"网络窗口");
Thread thread1=new Thread(windows,"实体窗口");
thread.start();
thread1.start();
}
}
class Ticket implements Runnable{
int ticket=150;
private ReentrantLock lock=new ReentrantLock();
@Override
public void run() {
for (int i = ticket; ticket>0; ticket--) {
try {
lock.lock();
if(ticket>0){
try {
Thread.sleep(100);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
System.out.println(Thread.currentThread().getName()+"正在卖票"+ticket+"张票");
}else {
break;
}
}finally {
lock.unlock();
}
}
}
}