概述
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多线程模拟抢票所遇到的程序开发问题。
如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。
本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
发表评论 取消回复