黑马程序员技术交流社区
标题:
多线程的问题
[打印本页]
作者:
rolling-stone
时间:
2014-8-1 22:43
标题:
多线程的问题
class Ticket implements Runnable
{
private int tick = 100;
Object obj = new Object();
public void run()
{
<font color="#ff0000"> while(true)</font>
{
synchronized(obj)
{
if(tick>0)
{
try{Thread.sleep(20);}catch(Exception e){}
System.out.println("sale tick..."+tick--);
}
}
}
}
}
class Demo8
{
public static void main(String[] args)
{
Ticket t = new Ticket();
Thread t1 = new Thread(t);
Thread t2 = new Thread(t);
Thread t3 = new Thread(t);
Thread t4 = new Thread(t);
t1.start();
t2.start();
t3.start();
t4.start();
}
}
复制代码
请问其中的While(true)的含义是什么?为什么要写这个?
作者:
王一勇
时间:
2014-8-1 23:05
如果不用while(true)的话,就卖出一张票,程序就停止了。
作者:
阿磊
时间:
2014-8-1 23:10
就是循环卖票,线程开起来后不结束
欢迎光临 黑马程序员技术交流社区 (http://bbs.itheima.com/)
黑马程序员IT技术论坛 X3.2