public class Test {
public static void main(String[] args) throws InterruptedException {
int time = 50;
int num = 50;
int people = 100;
TestThread t1 = new TestThread();
Thread[] thread = new Thread[people];
for (int i = 1; i < people; i++) {
thread[i] = new Thread(t1);
thread[i].start();
}
Thread.sleep(num * time);
System.out.println("tickets are over");
}
}
class TestThread implements Runnable {
int ticket = 50;
String str = " ";
int time = 50;
public void run() {
if (ticket > 0) {
synchronized (str) {
if (ticket > 0) {
System.out.println(Thread.currentThread().getName()
+ ": taker get " + ticket + " ticket");
ticket--;
}
}
}
try {
Thread.sleep(time);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
最后
以上就是慈祥绿茶最近收集整理的关于java多线程模拟抢票的全部内容,更多相关java多线程模拟抢票内容请搜索靠谱客的其他文章。
本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
发表评论 取消回复