- class Ticket implements Runnable
- {
- private int tick = 100;
- Object obj = new Object();
- public void run()
- {
- while(true)
- {
- synchronized(obj)
- {
- if(tick>0)
-
- try
- {
- Thread.sleep(1);
- }
- catch(Exception e)
- {
-
- }
-
- System.out.println(Thread.currentThread().getName()+"..."+tick--);
- }
-
- }
- }
- }
- class TicketTest
- {
- 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();
-
复制代码 同步代码块加上了为什么还会出现负数的票。求指点
|
|