package com.qyl.maipiao.one;
import java.util.concurrent.locks.ReentrantLock;
public class elderly extends Thread {
static int count = 50;
ReentrantLock lock = new ReentrantLock();
static Object obj = new Object();
@Override
public void run() {
while (count>0) {
lock.lock();
if(count>0){
try {
System.out.println(getName()+"售出第"+count);
count--;
} finally {
lock.unlock();
}
}
}
}
}
package com.qyl.maipiao.one;
public class count extends Exception {
public static void main(String[] args) {
elderly el = new elderly();
elderly e2 = new elderly();
elderly e3 = new elderly();
el.setName("老人");
e2.setName("小孩");
e3.setName("成人");
el.start();
e2.start();
e3.start();
}
}