import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;
public class lock {
public static void main(String[] args) {
lk to1=new lk();
lk to2=new lk();
Thread a1=new Thread(to1,"高铁");
Thread a2=new Thread(to1,"高铁");
a1.start();
a2.start();
}
}
class lk implements Runnable{
private Integer ti=100;
private Lock lock=new ReentrantLock();
@Override
public void run() {
while (true){
try {
lock.lock();
if (ti > 0) {
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println(Thread.currentThread().getName() + "卖" + ti + "票");
ti--;
}
}finally {
lock.unlock();
}
}
}
}
10-10
473