您的位置:首页 > 其它

14.3 线程的实现方式二练习:买票

2016-05-27 16:00 260 查看
class SaleTicke implements Runnable
{
int k=10; //注意这里不用静态,因为只创建一个Runable子类的对象
public void run()
{
while(true)

{
synchronized("锁")
{
if(k>0)
{
System.out.println(Thread.currentThread().getName()+"卖了第"+k+"张");
k--;
}
else
{
System.out.println("票卖完了");
break;
}
}
}
}
}

class wu
{
public static void main(String [] args)
{
SaleTicke st1 = new SaleTicke();
Thread th1 = new Thread(st1,"窗口一");
Thread th2 = new Thread(st1,"窗口二");
Thread th3 = new Thread(st1,"窗口三");
th1.start();
th2.start();
th3.start();

}

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: