您的位置:首页 > 其它

线程初学

2016-05-02 18:58 197 查看
1.哈尔滨火车站下面有三个火车票代售点,假如哈尔滨到北京的火车票总共是200张,如何用程序来实现三个售票点同时卖票的功能。

package Tread;

class PP implements Runnable{
public int tickets=200;
String str=new String("123");
public void run(){
while(true){
synchronized(this.str){

if(tickets>0){
System.out.println(Thread.currentThread().getName()+"卖出去的票数是:"+tickets--);
}
else
{
break;
}
}
}
}
}
public class Threade_14 {
public static void main(String[] args) {
PP pp =new PP();
Thread tt1=new Thread(pp);
Thread tt2=new Thread(pp);
Thread tt3=new Thread(pp);
tt1.start();
tt2.start();
tt3.start();
}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: