Java Multithreading: Join__java

Source: Internet
Author: User

Most of the content is based on official documents and online experience summary, through personal practice to organize and accumulate, only for reference.

The join () method is an instance method of the Java.lang.Thread class, and the method signature

Public final void Join () throws Interruptedexception
Public final void Join (Long Millis) throws Interruptedexception
Public final void Join (long millis, int nanos) throws Interruptedexception

The join () method causes one thread to execute at the end of another thread, and if invoked on one thread instance, the currently running thread will block until the thread instance calling the join () method ends

The join () method receives a long type parameter that causes the thread to fail after a specific millisecond is waiting

The join () method can also receive an extra int type parameter

Example:

public static void Main (string[] args) throws interruptedexception {
    thread t1 = new Thread (new Runnable () {
        @Over Ride public
        Void Run () {
            System.out.println ("A-thread started!");
            System.out.println ("Ready to sleep 5 seconds!");
            try {
                Thread.Sleep (5000);
            } catch (Interruptedexception e) {
                e.printstacktrace ();
            }
            System.out.println ("ended!");
    Thread t2 = new Thread (new Runnable () {
        @Override public
        void Run () {
            System.out.println ("Second Thread St Arted! ");
            System.out.println ("Second thread ended!");
        }
    );
    T1.start ();
    T1.join ();
    T2.start ();
}

If T1.join () is commented out, run the result:

To run the result after removing the annotation:

Pass a long type parameter to the join () method so that the maximum waits for 3 seconds

public static void Main (string[] args) throws interruptedexception {
    thread t1 = new Thread (new Runnable () {
        @Over Ride public
        Void Run () {
            System.out.println ("A-thread started!");
            System.out.println ("Ready to sleep 5 seconds!");
            try {
                Thread.Sleep (5000);
            } catch (Interruptedexception e) {
                e.printstacktrace ();
            }
            System.out.println ("ended!");
    Thread t2 = new Thread (new Runnable () {
        @Override public
        void Run () {
            System.out.println ("Second Thread St Arted! ");
            System.out.println ("Second thread ended!");
        }
    );
    T1.start ();
    T1.join (3000);
    T2.start ();
}

Run Result:

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.