- class Tra implements Runnable//extends Thread
- {
- Object k=new Object();
-
- private int tickets=100;
- boolean flag=true;
- public void run()
- {
- if(flag)
- {
- while(true){
- synchronized(this)
- {
- if(tickets>0)
- {
- try{Thread.sleep(10);}
- catch(Exception e){}
- System.out.println(Thread.currentThread().getName()+">>true>>"+tickets--);
- }
- }
- }
- }
- else
- while(true)
- show();
- }
- public synchronized void show()
- {
- if(tickets>0)
- {
- try{Thread.sleep(10);} catch(Exception e){}
- System.out.println(Thread.currentThread().getName()+">>false>>"+tickets--);
- }
- }
- }
- public class Test
- {
- public static void main(String args[])
- {
-
- Tra a=new Tra();
- Thread a1=new Thread(a,"a1");
- Thread a2=new Thread(a,"a2");
-
- a1.start();
- try{Thread.sleep(10);}catch(Exception e){}
- a.flag=false;
- a2.start();
- }
- }
复制代码 就是这个代码,谁解释一下??? |