Java Thread类的最终void join()方法与示例

线程类最终void join() (Thread Class final void join())

  • This method is available in package java.lang.Thread.join().

    软件包java.lang.Thread.join()中提供了此方法。

  • join() method is applicable when a thread wants to wait until completing some other thread then we should go for join() method of Thread class.

    join()方法 ,当一个线程要等到完成一些其他线程那么我们就应该去参加()Thread类的方法,方法是适用的。

  • This method is not static so we cannot access this method with the class name too.

    此方法不是静态的,因此我们也无法使用类名访问此方法。

  • This method is final we can't override this method in child class.

    此方法是最终方法,我们不能在子类中覆盖此方法。

  • The return type of this method is void so it does not return anything.

    此方法的返回类型为void,因此它不返回任何内容。

  • This method throws an InterruptedException so it is needed to handle exception either by try-catch or throws otherwise we will get a compile-time error.

    该方法抛出InterruptedException异常,因此需要通过try-catch或throws来处理异常,否则我们将获得编译时错误。

For example: We have three threads [t1 – PreparedExamPaper ] ,[t2 – PrintingExamPaper],[t3- DistributingExamPaper] so will see what will happen.

例如:我们有三个线程[ t1 – PreparedExamPaper],[ t2 – PrintingExamPaper],[ t3 -DistributingExamPaper],因此将看到会发生什么。

Let suppose if a thread t1 executes, t2.join(), then thread t1 will entered into waiting for the state until t2 completes once t2 completes then t1 will continue its execution.

假设如果线程t1执行t2.join() ,则线程t1将进入等待状态,直到t2完成,一旦t2完成,则t1将继续执行。

Similarly, If a thread t2 executes, t3.join() then thread t2 will entered into waiting for the state until t3 completes once t3 completes, then t2 will continue its execution.

同样,如果线程t2执行t3.join(),则线程t2将进入等待状态,直到t3完成后t3完成,然后t2将继续执行。

Syntax:

句法:

    final void join(){
    }

Parameter(s):

参数:

When we write t2.join(), so this line means currently executing thread will stop its execution and that thread will wait() for t2 completion.

当我们编写t2.join()时 ,此行表示当前正在执行的线程将停止执行,并且该线程将等待( 2)完成t2 。

Return value:

返回值:

The return type of this method is void, it does not return anything.

此方法的返回类型为void ,它不返回任何内容。

Java程序演示join()方法的示例 (Java program to demonstrate example of join() method)

/*  We will use Thread class methods so we are importing 
    the package but it is not mandated because 
    it is imported by default
*/

import java.lang.Thread;

class MyThread extends Thread {
    //Override run() method of Thread class 
    public void run() {
        for (int i = 0; i < 5; ++i)
            System.out.println("We are in MyThread");
        try {
            Thread.sleep(500);
        } catch (Exception ex) {
            System.out.println(ex.getMessage());
        }
    }
}

class MainThread {
    public static void main(String[] args) throws Exception {
        MyThread mt = new MyThread();
        mt.start();

        /* Note -1*/
        mt.join();

        for (int j = 0; j < 5; ++j)
            System.out.println("We are in MainThread");
    }
}

Note 1: If we comment /*mt.join()*/ in the above program then both threads will execute simultaneously we can't expect exact execution order and then we can't expect exact output.

注1:如果在上面的程序中注释/*mt.join()*/,那么两个线程将同时执行,我们不能期望确切的执行顺序,也不能期望精确的输出。

Output

输出量

E:\Programs>javac MainThread.java

E:\Programs>java MainThread
We are in MyThread
We are in MyThread
We are in MyThread
We are in MyThread
We are in MyThread
We are in MainThread
We are in MainThread
We are in MainThread
We are in MainThread
We are in MainThread


翻译自: https://www.includehelp.com/java/thread-class-final-void-join-method-with-example.aspx

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值