Java Multithreading-Merging
The meaning of a thread merge is that several parallel threads are merged into one single thread execution, which is applied when one of the threads must wait for the other thread to finish executing
Example: Existing a main thread, t1,t2 how to let T2 after T1 execution, the main thread after the T2 execution
Package Cn.thread;public class Threadjoin {public static void main (string[] args) {Threadjoin join = new Threa Djoin (); Thread T1 = join.new MyThread1 (); Thread t2 = new Thread (join.new myrunnable ()); T1.start (); T2.start (); for (int i = 0; i < 5; i++) {if (i==0) try {//T1 thread merged into the main thread, main line Process to execute the T1 thread until T1 execution is complete. T1.join (); T2 threads are merged into the main thread, the main thread stops executing the process, executes the T1 thread, executes the T2 thread t2.join (); } catch (Interruptedexception e) {e.printstacktrace (); } System.out.println ("Mainline Cheng Di" + i + "Execution! "); }} class MyThread1 extends Thread {public void run () {for (int i = 0; i < 5; i++) { SYSTEM.OUT.PRINTLN ("Thread 1" + i + "time to execute!) "); }}} class myrunnable implements runnable{public void run () {for (int i = 0;i < 5;i++) {System.out.println ("Thread 2," +i+ "executes!) "); } } }}
Java Multithreading-Merging