本帖最后由 ぺsimon☆ 于 2013-4-21 11:18 编辑
- 代码如下
- /**
- 需求:简单的买票程序
- 多个窗口买票
- */
- class Ticket extends Thread
- {
- private static int t=100;
- public void run()
- {
- while(true)
- {
- if(t>0)
- {
- System.out.println(Thread.currentThread().getName()+"sale:"+t--);
- }
- }
- }
- }
- class TicketDemo
- {
- public static void main(String[] args)
- {
- Ticket t1=new Ticket();
- Ticket t2=new Ticket();
- Ticket t3=new Ticket();
- Ticket t4=new Ticket();
- t1.start();
- t2.start();
- t3.start();
- t4.start();
- }
- }
复制代码 问题:下面这句话是怎么运行的呢?怎么打印结果有 Thread-2sale:100
while(true)
{
if(t>0)
{
System.out.println(Thread.currentThread().getName()+"sale:"+t--);
}
我的理解是t=100时,判断当whie是真运行下面代码,判断t>0,执行输出代码:System.out.println(Thread.currentThread().getName()+"sale:"+t--);
输出代码不是:t--不是输出99吗,怎么变了100 的呢? 100是怎么来的呢?
|