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