Java Multithreading-Merging

Source: Internet
Author: User

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 &LT 5;i++) {System.out.println ("Thread 2," +i+ "executes!)          "); }    }    }}




Java Multithreading-Merging

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.