本帖最后由 大米 于 2013-9-8 10:13 编辑
- class Ticket extends Thread
- {
- private static int tick=100;
- public void run()
- {
- //while(true)
- //{
- if (tick>0)
- {
- System.out.println(currentThread().getName()+"+++sale++"+ tick--);
-
- }
- //}
- }
- }
- class SaleTicket
- {
- 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();
- }
- }
复制代码 不加true语句为什么只卖4张票?
追加问题:代码这么改- class Ticket extends Thread
- {
- private static int tick=100;
- public void run()
- {
- //while(true)
- //{
- if (tick>0)
- {
- System.out.println(currentThread().getName()+"+++sale++"+ tick);
- tick--;
-
- }
- //}
- }
- }
- class SaleTicket
- {
- 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();
- }
- }
复制代码 运行结果这样? 不是应该 100 99 98 97么?
|
|