当启动线程1和线程2的时候,这时通过join()把主线程交给线程1来运行,之后线程1进入无限等待,这个时候线程2还在执行,就可以通过线程2来使用interrupt()方法唤醒主函数,从而线程2和主函数交替执行,这样就可以完成执行所有语句。如果线程1和线程2执行同一个方法,就要在进入无限等待前使用interrupt()方法唤醒主函数。 
这就是我最近问题的总结,附件中有两组代码,希望对大家有帮助。  
 
 
 
- class Dem implements Runnable 
 
 - {
 
 -         private Thread main;
 
 -         public Dem(Thread main)
 
 -         {
 
 -              this.main = main; //获取主线程
 
  
-         }
 
  
 
 
-         public synchronized  void run()//t1线程执行的方法
 
 -         {        
 
 -                         try
 
 -                         {
 
 -                                 main.interrupt();//唤醒主线程
 
 -                                                 wait();                                
 
 -                                                 
 
 -                         }
 
 -                         catch(InterruptedException e)
 
 -                         {
 
 -                                 System.out.println(Thread.currentThread().getName()+"---t1--Exception---");
 
  
-  
 
 -                         }
 
  
-                         System.out.println(Thread.currentThread().getName()+"---t1---RUN----");
 
 -         }
 
 - }
 
  
 
 
- class InterruptDemo
 
 - {
 
 -         public static void main(String[] args) throws Exception
 
 -         {
 
  
-                 Thread main = Thread.currentThread();//获得主线程的引用。
 
 -   
 
  
-                 Dem d=new Dem(main);
 
 -                 
 
 -                 Thread t1=new Thread(d);
 
 -                 Thread t2=new Thread(d);
 
  
 
-                 t1.start();
 
 -                 t2.start();
 
 -                   try
 
 -                  {
 
 -                                         t1.join();
 
 -                  }
 
 -                  catch (InterruptedException e)
 
 -                  {
 
 -                                          System.out.println(Thread.currentThread().getName()+"---main--Exception---");
 
 -                  }
 
 -                 for(int x=0;x<40;x++)
 
 -                 {
 
  
-                         System.out.println(Thread.currentThread().getName()+"---main-----"+x);        
 
  
-                 }
 
  
-                 System.out.println("over");
 
 -                                 t1.interrupt();
 
 -                                 t2.interrupt();
 
 -         }
 
 - }
 
  复制代码 
 
 
 |   
        
 
    
    
    
    
 
 
 |