Java Thread Execution sequence control

Source: Internet
Author: User
		thread T1 = new Thread (new Runnable () {

			@Override public
			void Run () {
				System.out.println ("T1");
			}
		); Thread
		t2 = new Thread (new Runnable () {

			@Override public
			void Run () {
				try {
					//reference T1 thread, waiting for T1 thread to finish executing 
 t1.join ();
				} catch (Interruptedexception e) {
					e.printstacktrace ();
				}
				System.out.println ("T2");
			}
		);
		thread t3 = new Thread (new Runnable () {

			@Override public
			void Run () {
				try {
					//reference t2 thread, waiting for the T2 thread to finish
					executing T2.join ();
				} catch (Interruptedexception e) {
					e.printstacktrace ();
				}
				System.out.println ("T3");
			}
		);
		T3.start ();
		T2.start ();
		T1.start ();

If there are three threads t1,t2,t3, how to ensure that T2 is performed before T3, T1 done before T2.

The Join method is used to refer to threads in this thread that need to be joined in execution, so that the incoming thread can be executed first.

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.