多线程知识

    1. T1,T2,T3三个线程工作顺序,按照T1,T2,T3依次进行
    2. public class T1 implements Runnable{   
    3.   
    4.     @Override  
    5.     public void run() {   
    6.         try {   
    7.             System.out.println("T1开始工作.....");   
    8.             Thread.sleep(RandomUtils.nextInt(300));   
    9.             System.out.println("T1结束工作>>>>>");   
    10.         } catch (InterruptedException e) {   
    11.             e.printStackTrace();   
    12.         }   
    13.     }   
    14.   
    15. }  
  1. public class Main {   
  2.   
  3.     public static void main(String[] args) throws InterruptedException {   
  1.            
  2.         Thread t1 = new Thread(new T1());   
  3.         Thread t2 = new Thread(new T2());   
  4.         Thread t3 = new Thread(new T3());   
  5.            
  6.         t1.start();   
  7.         t1.join();   
  8.            
  9.         t2.start();   
  10.         t2.join();   
  11.            
  12.         t3.start();   
  13.         t3.join();   
  14.            
  15.         System.out.println("T1、T2、T3依次工作结束.");   
  16.     }   
  17.   
  18. }  
  19. 加入join方法之后T1,T2,T3不再是无序的线程,按照T1,T2,T3顺序执行了。

猜你喜欢

转载自cuishikuan.iteye.com/blog/2225144