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()方法 ,當(dāng)一個(gè)線程要等到完成一些其他線程那么我們就應(yīng)該去參加()Thread類的方法,方法是適用的。
This method is not static so we cannot access this method with the class name too.
此方法不是靜態(tài)的,因此我們也無(wú)法使用類名訪問此方法。
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,因此它不返回任何內(nèi)容。
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來(lái)處理異常,否則我們將獲得編譯時(shí)錯(cuò)誤。
For example: We have three threads [t1 – PreparedExamPaper ] ,[t2 – PrintingExamPaper],[t3- DistributingExamPaper] so will see what will happen.
例如:我們有三個(gè)線程[ t1 – PreparedExamPaper],[ t2 – PrintingExamPaper],[ t3 -DistributingExamPaper],因此將看到會(huì)發(fā)生什么。
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.
假設(shè)如果線程t1執(zhí)行t2.join() ,則線程t1將進(jìn)入等待狀態(tài),直到t2完成,一旦t2完成,則t1將繼續(xù)執(zhí)行。
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執(zhí)行t3.join(),則線程t2將進(jìn)入等待狀態(tài),直到t3完成后t3完成,然后t2將繼續(xù)執(zhí)行。
Syntax:
句法:
final void join(){}Parameter(s):
參數(shù):
When we write t2.join(), so this line means currently executing thread will stop its execution and that thread will wait() for t2 completion.
當(dāng)我們編寫t2.join()時(shí) ,此行表示當(dāng)前正在執(zhí)行的線程將停止執(zhí)行,并且該線程將等待( 2)完成t2 。
Return value:
返回值:
The return type of this method is void, it does not return anything.
此方法的返回類型為void ,它不返回任何內(nèi)容。
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()*/,那么兩個(gè)線程將同時(shí)執(zhí)行,我們不能期望確切的執(zhí)行順序,也不能期望精確的輸出。
Output
輸出量
E:\Programs>javac MainThread.javaE:\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
總結(jié)
以上是生活随笔為你收集整理的Java Thread类的最终void join()方法与示例的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: SpringBoot 热部署神器快速重启
- 下一篇: java getmonth_Java L