Java 指定线程执行顺序(三种方式)

博主地址:http://blog.****.net/difffate

*方法二 *方法三

     方法一:通过共享对象锁加上可见变量来实现。

  1. public class MyService {  
  2.   
  3.     private volatile int orderNum = 1;  
  4.   
  5.     public synchronized void methodA() {  
  6.         try {  
  7.             while (orderNum != 1) {  
  8.                 wait();  
  9.             }  
  10.             for (int i = 0; i < 2; i++) {  
  11.                 System.out.println(”AAAAA”);  
  12.             }  
  13.             orderNum = 2;  
  14.             notifyAll();  
  15.         } catch (InterruptedException e) {  
  16.             e.printStackTrace();  
  17.         }  
  18.     }  
  19.   
  20.     public synchronized void methodB() {  
  21.         try {  
  22.             while (orderNum != 2) {  
  23.                 wait();  
  24.             }  
  25.             for (int i = 0; i < 2; i++) {  
  26.                 System.out.println(”BBBBB”);  
  27.             }  
  28.             orderNum = 3;  
  29.             notifyAll();  
  30.         } catch (InterruptedException e) {  
  31.             e.printStackTrace();  
  32.         }  
  33.     }  
  34.   
  35.     public synchronized void methodC() {  
  36.         try {  
  37.             while (orderNum != 3) {  
  38.                 wait();  
  39.             }  
  40.             for (int i = 0; i < 2; i++) {  
  41.                 System.out.println(”CCCCC”);  
  42.             }  
  43.             orderNum = 1;  
  44.             notifyAll();  
  45.         } catch (InterruptedException e) {  
  46.             e.printStackTrace();  
  47.         }  
  48.     }  
  49. }  

  1. import service.MyService;  
  2. public class ThreadAA extends Thread {  
  3.   
  4.     private MyService dbtools;  
  5.   
  6.     public ThreadAA(MyService dbtools) {  
  7.         super();  
  8.         this.dbtools = dbtools;  
  9.     }  
  10.   
  11.     @Override  
  12.     public void run() {  
  13.         dbtools.methodA();  
  14.     }  
  15.   
  16. }  

  1. import service.MyService;  
  2. public class ThreadBB extends Thread {  
  3.   
  4.     private MyService dbtools;  
  5.   
  6.     public ThreadBB(MyService dbtools) {  
  7.         super();  
  8.         this.dbtools = dbtools;  
  9.     }  
  10.   
  11.     @Override  
  12.     public void run() {  
  13.         dbtools.methodB();  
  14.     }  
  15.   
  16. }  

  1. import service.MyService;  
  2. public class ThreadCC extends Thread {  
  3.   
  4.     private MyService dbtools;  
  5.   
  6.     public ThreadCC(MyService dbtools) {  
  7.         this.dbtools = dbtools;  
  8.     }  
  9.   
  10.     @Override  
  11.     public void run() {  
  12.         dbtools.methodC();  
  13.     }  
  14.   
  15. }  

  1. import extthread.ThreadCC;  
  2. import service.MyService;  
  3. import extthread.ThreadAA;  
  4. import extthread.ThreadBB;  
  5.   
  6. public class Run {  
  7.   
  8.     public static void main(String[] args) {  
  9.         MyService myService = new MyService();  
  10.         for (int i = 0; i < 2; i++) {  
  11.             ThreadBB output = new ThreadBB(myService);  
  12.             output.start();  
  13.             ThreadAA input = new ThreadAA(myService);  
  14.             input.start();  
  15.             ThreadCC threadCC = new ThreadCC(myService);  
  16.             threadCC.start();  
  17.         }  
  18.     }  
  19.   
  20. }  

执行结果:

Java 指定线程执行顺序(三种方式)

可以看到线程的启动按顺序执行了。共享对象锁,可以保证每个方法只能同时有一个线程进入,配合wait和notifyall方法,可以启动或者唤醒线程。


     方法二:通过主线程Join()

  1. class T11 extends Thread {  
  2.     public void run() {  
  3.         System.out.println(”in T1”);  
  4.     }  
  5. }  
  6.   
  7. class T22 extends Thread {  
  8.     public void run() {  
  9.         System.out.println(”in T2”);  
  10.     }  
  11. }  
  12.   
  13. class T33 extends Thread {  
  14.     public void run() {  
  15.         System.out.println(”in T3”);  
  16.     }  
  17. }  
  18.   
  19. public class Test2 {  
  20.     public static void main(String[] args) throws InterruptedException {  
  21.         T11 t1 = new T11();  
  22.         T22 t2 = new T22();  
  23.         T33 t3 = new T33();  
  24.         t1.start();  
  25.         t1.join();  
  26.         t2.start();  
  27.         t2.join();  
  28.         t3.start();  
  29.     }  
  30. }  

   方法三:通过线程执行时Join()

  1. class T1 extends Thread {  
  2.     public void run(){  
  3.         Random random = new Random();  
  4.         try {  
  5.             Thread.sleep(random.nextInt(1000));  
  6.         } catch (InterruptedException e) {  
  7.             e.printStackTrace();  
  8.         }  
  9.         System.out.println(”in T1”);  
  10.     }  
  11. }  
  12.   
  13. class T2 extends Thread{  
  14.     private Thread thread;  
  15.     public T2(Thread thread) {  
  16.         this.thread = thread;  
  17.     }  
  18.   
  19.     public void run(){  
  20.         try {  
  21.             thread.join();  
  22.         } catch (InterruptedException e) {  
  23.             e.printStackTrace();  
  24.         }  
  25.         System.out.println(”in T2”);  
  26.     }  
  27. }  
  28.   
  29. class T3 extends Thread{  
  30.     private Thread thread;  
  31.     public T3(Thread thread) {  
  32.         this.thread = thread;  
  33.     }  
  34.   
  35.     public void run(){  
  36.         try {  
  37.             thread.join();  
  38.         } catch (InterruptedException e) {  
  39.             e.printStackTrace();  
  40.         }  
  41.         System.out.println(”in T3”);  
  42.     }  
  43. }  
  44.   
  45. public class Test {  
  46.     public static void main(String[] args) throws InterruptedException {  
  47.         T1 t1 = new T1();  
  48.         T2 t2 = new T2(t1);  
  49.         T3 t3 = new T3(t2);  
  50.         t2.start();  
  51.         t1.start();  
  52.         t3.start();  
  53.     }  
  54. }