Learn from the java-18.2 main threading mechanism (7) Join

Source: Internet
Author: User

Let's talk about join in this chapter.

1. What is a join?

Blocks the current thread and lets the. Join thread finish first. And then continue to be the front line.

The following is an explanation of the API:

A.join, the explanation in the API is to block the current thread B until a runs out and dies, and then runs B.



2. Examples:

Package Com.ray.ch17;public class Test {public static void main (string[] args) throws Interruptedexception {SYSTEM.OUT.PR Intln (Thread.CurrentThread (). GetName () + "start"); Sleepthread sleepthread = new Sleepthread ("Sleep"); Thread.Sleep (2000); Jointhread jointhread = new Jointhread (sleepthread, "join");//jointhread.join (); System.out.println (Thread.CurrentThread (). GetName () + "End");}} Class Sleepthread extends Thread {public sleepthread (String name) {super (name); start ();} @Overridepublic void Run () {try {for (int i = 0; i < 5; i++) {System.out.println (GetName () + "Sleepthread"); Thread.Sleep (1000);}} catch (Interruptedexception e) {e.printstacktrace ();}}} Class Jointhread extends Thread {private sleepthread sleepthread;public jointhread (sleepthread sleepthread, String name) {super (name); this.sleepthread = Sleepthread;start ();} @Overridepublic void Run () {try {System.out.println (GetName () + "join start"); Sleepthread.join (); System.out.println (GetName () + "Join End");} catch (InterruptedeXception e) {e.printstacktrace ();}}} 

Output:

Main start
Sleep Sleepthread
Sleep Sleepthread
Sleep Sleepthread
Main end
Join Join Start
Sleep Sleepthread
Sleep Sleepthread
Join Join End


Package Com.ray.ch17;public class Test {public static void main (string[] args) throws Interruptedexception {SYSTEM.OUT.PR Intln (Thread.CurrentThread (). GetName () + "start"); Sleepthread sleepthread = new Sleepthread ("Sleep"); Thread.Sleep (2000); Jointhread jointhread = new Jointhread (sleepthread, "join"); Jointhread.join (); System.out.println (Thread.CurrentThread (). GetName () + "End");}} Class Sleepthread extends Thread {public sleepthread (String name) {super (name); start ();} @Overridepublic void Run () {try {for (int i = 0; i < 5; i++) {System.out.println (GetName () + "Sleepthread"); Thread.Sleep (1000);}} catch (Interruptedexception e) {e.printstacktrace ();}}} Class Jointhread extends Thread {private sleepthread sleepthread;public jointhread (sleepthread sleepthread, String name) {super (name); this.sleepthread = Sleepthread;start ();} @Overridepublic void Run () {try {System.out.println (GetName () + "join start"); Sleepthread.join (); System.out.println (GetName () + "Join End");} catch (InterruptedexcEption e) {e.printstacktrace ();}}} 

Output:

Main start
Sleep Sleepthread
Sleep Sleepthread
Sleep Sleepthread
Join Join Start
Sleep Sleepthread
Sleep Sleepthread
Join Join End
Main end


The difference between the above two codes is mainly the body now jointhread.join ();

The main difference between the output of the above two code is the position of main end. When Jointhread.join (), because the thread of the main method needs to be blocked first, the Jointhread is completed, so the position of main end will change.

There is one more aspect. The position of the text we output from sleep and join two threads can also deduce the above conclusion.


Summary: In this chapter we have a brief introduction to join.


This chapter is here, thank you.

-----------------------------------

Folder


Learn from the java-18.2 main threading mechanism (7) Join

Related Article

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.