轉載:https://blog.csdn.net/eene894777/article/details/74942485
方法一:通過共享對象鎖加上可見變量來實現。
public class MyService { private volatile int orderNum = 1; public synchronized void methodA() { try { while (orderNum != 1) { wait(); } for (int i = 0; i < 2; i++) { System.out.println("AAAAA"); } orderNum = 2; notifyAll(); } catch (InterruptedException e) { e.printStackTrace(); } } public synchronized void methodB() { try { while (orderNum != 2) { wait(); } for (int i = 0; i < 2; i++) { System.out.println("BBBBB"); } orderNum = 3; notifyAll(); } catch (InterruptedException e) { e.printStackTrace(); } } public synchronized void methodC() { try { while (orderNum != 3) { wait(); } for (int i = 0; i < 2; i++) { System.out.println("CCCCC"); } orderNum = 1; notifyAll(); } catch (InterruptedException e) { e.printStackTrace(); } } }
import service.MyService; public class ThreadAA extends Thread { private MyService dbtools; public ThreadAA(MyService dbtools) { super(); this.dbtools = dbtools; } @Override public void run() { dbtools.methodA(); } }
import service.MyService; public class ThreadBB extends Thread { private MyService dbtools; public ThreadBB(MyService dbtools) { super(); this.dbtools = dbtools; } @Override public void run() { dbtools.methodB(); } }
import service.MyService; public class ThreadCC extends Thread { private MyService dbtools; public ThreadCC(MyService dbtools) { this.dbtools = dbtools; } @Override public void run() { dbtools.methodC(); } }
import extthread.ThreadCC; import service.MyService; import extthread.ThreadAA; import extthread.ThreadBB; public class Run { public static void main(String[] args) { MyService myService = new MyService(); for (int i = 0; i < 2; i++) { ThreadBB output = new ThreadBB(myService); output.start(); ThreadAA input = new ThreadAA(myService); input.start(); ThreadCC threadCC = new ThreadCC(myService); threadCC.start(); } } }
方法二:通過主線程Join()
class T11 extends Thread { public void run() { System.out.println("in T1"); } } class T22 extends Thread { public void run() { System.out.println("in T2"); } } class T33 extends Thread { public void run() { System.out.println("in T3"); } } public class Test2 { public static void main(String[] args) throws InterruptedException { T11 t1 = new T11(); T22 t2 = new T22(); T33 t3 = new T33(); t1.start(); t1.join(); t2.start(); t2.join(); t3.start(); } }
方法三:通過線程執行時Join()
class T1 extends Thread { public void run(){ Random random = new Random(); try { Thread.sleep(random.nextInt(1000)); } catch (InterruptedException e) { e.printStackTrace(); } System.out.println("in T1"); } } class T2 extends Thread{ private Thread thread; public T2(Thread thread) { this.thread = thread; } public void run(){ try { thread.join(); } catch (InterruptedException e) { e.printStackTrace(); } System.out.println("in T2"); } } class T3 extends Thread{ private Thread thread; public T3(Thread thread) { this.thread = thread; } public void run(){ try { thread.join(); } catch (InterruptedException e) { e.printStackTrace(); } System.out.println("in T3"); } } public class Test { public static void main(String[] args) throws InterruptedException { T1 t1 = new T1(); T2 t2 = new T2(t1); T3 t3 = new T3(t2); t2.start(); t1.start(); t3.start(); } }
方法四,使用線程池newSingleThreadExecutor()
package com.zyh.controller.test; import java.util.concurrent.ExecutorService; import java.util.concurrent.Executors; /** * student threads * * @author 1101399 * @CreateDate 2018-7-30 下午2:09:01 */ public class ThreadTestOne { public static void main(String[] args) throws InterruptedException { final ThreadTest A1 = new ThreadTest("A1"); final ThreadTest A2 = new ThreadTest("A2"); final ThreadTest A3 = new ThreadTest("A3"); // A1.start(); TODO 屏蔽 // A2.start(); TODO 屏蔽 A1.join(); A2.join(); System.out.println("方法一實現多線程"); if (!A1.isAlive()) {// A1 線程不存在的時候控制台打印一條信息 System.out.println("A1執行完畢?!"); } final Thread B1 = new Thread(new RunnableTest("B1")); // B1.start(); TODO 屏蔽 final Thread B2 = new Thread(new RunnableTest("B2")); // B2.start(); TODO 屏蔽 B1.join(); B2.join(); System.out.println("方法二實現多線程"); /** * 直接實現線程的開辟 FIXME */ final Thread C1 = new Thread(new Runnable() { @Override public void run() { try { A1.join(); A2.join(); B1.join(); B2.join(); } catch (InterruptedException e) { e.printStackTrace(); } for (int i = 0; i < 15; i++) { System.out.println("··············"); } } }); // C1.start(); TODO 屏蔽 C1.join(); System.out.println("方法三實現多線程"); System.out.println("線程池的應用"); // 線程池的學習&應用 TODO 依次執行 ExecutorService executor = Executors.newSingleThreadExecutor(); executor.submit(A1); executor.submit(A2); executor.submit(A3); executor.execute(B1);// 這種樣子的線程類就是不執行 executor.execute(A1); executor.submit(B1);// 這三種線程的實現方式之前不能 XXX start()啟動線程 executor.submit(B2);// 這三種線程的實現方式之前不能 XXX start()啟動線程 executor.submit(C1);// 這三種線程的實現方式之前不能 XXX start()啟動線程 executor.shutdown();// 停止傳入任務 // executor.shutdownNow();// 停止線程池-對線程池說STOP // 會導致線程池中第一個線程的sleep出現sleep interrupted異常 // 該函數的核心是:它向該線程發起interrupt()請求,而sleep()方法遇到有interrupt()請求時,會拋出InterruptedException(),並繼續往下執行 // 運行到這條語句直接停止線程池-檢測線程停止后執行的join()函數毫無意義,不能生效 } /** * 繼承Thread來實現多線程編程 FIXME */ public static class ThreadTest extends Thread { public String nameOne; public StringBuffer nameTwo; public StringBuilder nameThree; private long time; // 構造函數 public ThreadTest(String name) { this.nameOne = name; } // 構造函數 public ThreadTest(String name, long time) { this.nameOne = name; this.time = time; } @Override public void run() { for (int i = 0; i < 5; i++) { System.out.println(this.nameOne + " Thread運行第 " + i + " 次!"); try { if (this.time != 0) { sleep(this.time + i); System.out.println(this.nameOne + "-time-" + (time + i)); } else { // sleep((int) Math.random() * 1000); sleep(50); System.out .println(this.nameOne + "-random-" + (int) (Math.random() * 1000)); } } catch (InterruptedException e) { e.printStackTrace(); } } } } /** * 實現接口Runnable來實現多線程編程 FIXME */ public static class RunnableTest implements Runnable { public String nameOne; public StringBuffer nameTwo; public StringBuilder nameThree; private long time; public RunnableTest(String name) { this.nameOne = name; } public RunnableTest(String name, long time) { this.nameOne = name; this.time = time; } @Override public void run() { for (int i = 0; i < 5; i++) { System.out.println(this.nameOne + " Runnable運行第 " + i + " 次!"); try { if (this.time != 0) { Thread.sleep(this.time + i); System.out.println(this.nameOne + "-time-" + (time + i)); } else { Thread.sleep((int) Math.random() * 1000); System.out .println(this.nameOne + "-random-" + (int) (Math.random() * 1000)); } } catch (InterruptedException e) { e.printStackTrace(); } } } } }
newSingleThreadExecutor()單線程化線程池
final Thread B1 = new Thread(new RunnableTest("B1")); B1.start();// TODO 屏蔽 final Thread B2 = new Thread(new RunnableTest("B2")); B2.start();// TODO 屏蔽 final Thread C1 = new Thread(new Runnable() { @Override public void run() { try { A1.join(); A2.join(); B1.join(); B2.join(); } catch (InterruptedException e) { e.printStackTrace(); } for (int i = 0; i < 2; i++) { System.out.println("··············"); } } }); C1.start();// TODO 屏蔽 executor.submit(B1);// 這三種線程的實現方式之前不能 XXX start()啟動線程 executor.submit(B2);// 這三種線程的實現方式之前不能 XXX start()啟動線程 executor.submit(C1);// 這三種線程的實現方式之前不能 XXX start()啟動線程
