定义规则类
package p3;
public class SaleTicket implements Runnable {
public int num = 5;
@Override
public void run() {
Thread thread = Thread.currentThread();
String name = thread.getName();
while (num > 0) {
num--;
String fmt = "%s卖掉了一张票,余票为%s";
String msg = String.format(fmt, name, num);
System.out.println(msg);
}
}
}
定义测试类
package p3;
public class Test {
public static void main(String[] args) {
SaleTicket saleTicket = new SaleTicket();
Thread gy = new Thread(saleTicket, "关羽");
Thread zf = new Thread(saleTicket, "张飞");
gy.start();
zf.start();
}
}