java的线程一道题! 有100张票四个售票窗口一个售票的代表一个线程帮我写下 谢谢 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 思路:用一个ExcutorServer启动100个线程,在线程的run方法中对一个线程安全的集合进行读写操作 ExcutorServer es=Executors.newCachedThreadPool();for(int i=0;i<100;i++)es.execute(new Tread());run方法里用一个线程安全的集合去存储那些票,就可以保证同步如List list = Collections.synchronizedList(new ArrayList()); public class TestThread extends Thread { private static int tickets = 400; private String name; private int selled; public TestThread(String name) { this.name = name; } public void run() { while (tickets > 0) { tickets--; selled++; System.out.println(name + "售出了" + selled + "张票, 还剩" + tickets + "张票。"); try { this.currentThread().sleep(500); } catch (InterruptedException e) { // TODO Auto-generated catch block e.printStackTrace(); } } } public static void main(String[] args) { for (int i = 0; i < 4; i++) { new TestThread("售票窗口" + i).start(); } }} 有关Properties 问题 跪求!用JAVA实现对文本文件的修改 不用数据库,怎么实现数据读写。 java 引入自定义包的问题 我不想每个stmt都设置超时 菜鸟做作业,问个连数据库的问题!超简单 怎样弹出提示窗口? 请教一下程序员考试的情况 我的java怎么会这样? 请教高手一个颇有难度的问题。。。 055.130--还是线程 055.171--垃圾回收
for(int i=0;i<100;i++)
es.execute(new Tread());run方法里用一个线程安全的集合去存储那些票,就可以保证同步
如List list = Collections.synchronizedList(new ArrayList());
public class TestThread extends Thread { private static int tickets = 400;
private String name;
private int selled; public TestThread(String name) {
this.name = name;
} public void run() {
while (tickets > 0) {
tickets--;
selled++;
System.out.println(name + "售出了" + selled + "张票, 还剩" + tickets
+ "张票。");
try {
this.currentThread().sleep(500);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
} public static void main(String[] args) {
for (int i = 0; i < 4; i++) {
new TestThread("售票窗口" + i).start();
}
}}