看一下这段代码,为什么每次运行时都是0线程在走而其他线程都不走呢?
- class ThreadDemo4
- {
- 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();
- }
- }
- class Ticket implements Runnable
- {
- private int num = 1000;
- Object obj = new Object();
- public void run()
- {
- show();
- }
- public void show()
- {
- while (true)
- {
- synchronized(obj)
- {
- if (num>0)
- {
- try
- {
- Thread.sleep(20);
- }
- catch (InterruptedException e)
- {
- e.printStackTrace();
- }
- System.out.println("线程名字:"+Thread.currentThread().getName()+"--num:"+ num--);
- }
- }
- }
- }
- }
复制代码 |
|