通过线程同步实现售票点售票问题,10个售票点,1000张票

天地一棵树 2010-04-16 08:44:41
/**
* @author 蒲公英
*
*/
public class sallTicket extends Thread {

/**
* @param args
*/
private int ticket=1000;
private String window;
public sallTicket(String window){
this.window=window;
}
// TODO Auto-generated method stub
public void run(){
PrintClass.printch(window);
while(true){
synchronized(this){
if(ticket>0){
try{
Thread.sleep(100);
}catch(InterruptedException e){
e.printStackTrace();
}

Thread t=Thread.currentThread();
System.out.println(t.getName()+"卖出车票"+ticket);
ticket--;
}else {
break;
}
}
}
}



public static void main(String[] args) {
// TODO Auto-generated method stub
Thread window1=new Thread("窗口1:");
Thread window2=new Thread("窗口2:");
Thread window3=new Thread("窗口3:");
Thread window4=new Thread("窗口4:");
Thread window5=new Thread("窗口5:");
Thread window6=new Thread("窗口6:");
Thread window7=new Thread("窗口7:");
Thread window8=new Thread("窗口8:");
Thread window9=new Thread("窗口9:");
Thread window10=new Thread("窗口10:");
window1.start();
window2.start();
window3.start();
window4.start();
window5.start();
window6.start();
window7.start();
window8.start();
window9.start();
window10.start();
System.out.println();
}
}
class PrintClass{
static Object printer=new Object();
public static void printch(String window){
synchronized(printer){
for(int i=1;i<10;i++){
System.out.println(window+" ");
try{
Thread.sleep(100);
}catch(InterruptedException e){
e.printStackTrace();
}
}
}
}
}
...全文
192 1 打赏 收藏 转发到动态 举报
写回复
用AI写文章
1 条回复
切换为时间正序
请发表友善的回复…
发表回复
byhsh 2010-04-16
  • 打赏
  • 举报
回复
怎么感觉楼主跟我一个班的,我们也有这作业!

58,454

社区成员

发帖
与我相关
我的任务
社区描述
Java Eclipse
社区管理员
  • Eclipse
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧