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

Source: Internet
Author: User

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

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.