实体类
public class Tickets {
private int num;
public Tickets() {
}
public Tickets(int num) {
this.num = num;
}
public int getNum() {
return num;
}
public void setNum(int num) {
this.num = num;
}
}
工具类
public class Tools implements Runnable{
private Tickets tickets;
public Tools() {
}
public Tools(Tickets tickets) {
this.tickets = tickets;
}
@Override
public void run() {
}
public static void sell(Tickets tickets){
int num = tickets.getNum();
while (num > 0) {
System.out.println(Thread.currentThread().getName()+"正在卖第 " + num + "张票" + "还剩" + --num + "张票");
}
}
测试类
public class Test {
public static void main(String[] args){
Tickets tickets = new Tickets(100);
Tools tools = new Tools(tickets);
new Thread(()->{
tools.sell(tickets);
},"1号").start();
new Thread(()->{
tools.sell(tickets);
},"2号").start();
}
}