62,615
社区成员
发帖
与我相关
我的任务
分享
public class ceshi {
public static void main(String[] args) {
// while(MyRunnable.NUM >= 0) {
// System.out.println("正在出售第" + MyRunnable.NUM-- + "张票");
// try {
// Thread.sleep(100);
// } catch (InterruptedException e) {
// // TODO Auto-generated catch block
// e.printStackTrace();
// }
// }
MyRunnable t1 = new MyRunnable("线程1");
MyRunnable t2 = new MyRunnable("线程2");
MyRunnable t3 = new MyRunnable("线程3");
t1.start();
t2.start();
t3.start();
}
}
class MyRunnable implements Runnable {
public static int NUM = 100;
private Thread t;
private String name;
public MyRunnable(String name) {
this.name = name;
}
public MyRunnable() {
}
@Override
public void run() {
while (true) {
synchronized ("1") {
if(NUM < 1) {
break;
}
System.out.println(name + ": 正在出售第" + NUM-- + "张票");
}
try {
Thread.sleep(100);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
public void start() {
if (t == null) {
t = new Thread(this, "name");
t.start();
}
}
}