package com.qyl.maipiao.one3;
import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;
public class eldrlyLock implements Runnable{
int count = 50;
private final ReentrantLock lock = new ReentrantLock();
@Override
public void run() {
while (count>0) {
lock.lock();
if (count>0) {
try {
System.out.println(Thread.currentThread().getName()+"正在销售"+count+"张票");
count--;
} finally {
lock.unlock();
}
}
}
}
}
package com.qyl.maipiao.one3;
public class count {
public static void main(String[] args) {
eldrlyLock eld = new eldrlyLock();
new Thread(eld,"老人").start();
new Thread(eld,"小孩").start();
new Thread(eld,"军人").start();
}
}