Let's talk about join in this chapter.
1. What is a join?
Plug the current thread, let the thread of the. Join finish first, and then proceed to the front
The following is an explanation of the API:
A.join, the explanation in the API is to plug the current thread B until a executes and dies, then executes B.
2. Example:
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 differences of the above two codes are mainly embodied in jointhread.join ();
Compared to the output of the above two code, the main difference is the position of main end, when Jointhread.join (), because the need to block the main method of the thread, the first completion of Jointhread, so the position of main end will appear above the change.
On the other hand, the position of the text that we output from sleep and join two threads can also be pushed to export the above conclusion.
Summary: This section provides a brief introduction to join.
This chapter is here, thank you.
-----------------------------------
Directory
Learn from the java-18.2 basic threading mechanism (7) Join