線程類最終同步無效連接(long time_in_ms) (Thread Class final synchronized void join(long time_in_ms))
This method is available in package java.lang.Thread.join(long time_in_ms).
軟件包java.lang.Thread.join(long time_in_ms)中提供了此方法。
join(long time_in_ms) method is applicable when currently executing thread wants to wait for a particular amount of time in milliseconds until completing some other thread then we should go for join(long time_in_ms) method of Thread class.
join(long time_in_ms)方法適用于當前正在執行的線程想要以毫秒為單位的特定時間,直到完成其他線程為止,然后我們應該使用Thread類的join(long time_in_ms)方法。
This method is synchronized that is only one thread is allowed to operate one object.
該方法是同步的,僅允許一個線程操作一個對象。
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 two threads [t1 – PreparedExamPaper], [t2 – PrintingExamPaper] so will see what will happen.
例如,我們有兩個線程[ t1 – PreparedExamPaper],[ t2 – PrintingExamPaper],因此將看到會發生什么。
Let suppose if a thread t1 executes, t2.join(1000), then thread t1 will entered into waiting state for 1000 milliseconds until t2 completes and suppose in case if t2 couldn't complete its execution in 1000 ms so in that case, t1 will get a chance to execute and if thread t1 goes into waiting state or sleep mode then again t2 will get a chance to execute its execution for 1000 ms and the same process will repeat.
假設如果線程t1執行了t2.join(1000) ,則線程t1將進入等待狀態1000毫秒,直到t2完成為止,并假設t2無法在1000 ms內完成執行,因此在這種情況下, t1線程t1將有機會執行,并且如果線程t1進入等待狀態或睡眠模式,則t2再次有機會執行其執行1000 ms,并且將重復相同的過程。
Syntax:
句法:
final synchronized void join(long time_in_ms){
}
Parameter(s):
參數:
When we write t2.join(2000), so this line means currently executing thread will stop its execution for 2000 milliseconds until t2 completion.
當我們編寫t2.join(2000)時 ,此行表示當前正在執行的線程將在2000毫秒內停止執行,直到t2完成。
Return value:
返回值:
The return type of this method is void, it does not return anything.
此方法的返回類型為void ,它不返回任何內容。
Java程序演示join(long time_in_ms)方法的示例 (Java program to demonstrate example of join(long time_in_ms) method)
/* We will use Thread class methods so we are importing
the package but it is not mandate 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("Thread started:" + Thread.currentThread().getName());
try {
Thread.sleep(500);
} catch (Exception ex) {
System.out.println(ex.getMessage());
}
}
System.out.println("Thread Ended :" + Thread.currentThread().getName());
}
}
class MainThread1 {
public static void main(String[] args) throws Exception {
MyThread mt = new MyThread();
mt.start();
/* Note -1*/
mt.join(1000);
for (int j = 0; j < 2; ++j)
System.out.println("Thread started:" + Thread.currentThread().getName());
System.out.println("Thread ended:" + Thread.currentThread().getName());
}
}
Note1 : Here, we have written /*mt.join(1000)*/ means currently executing thread [main] will give a chance to another thread named [MyThread mt] for 1000 ms and then after main thread will get a chance to execute and if main thread goes into waiting for state or sleep mode then again MyThread will get a chance for 1000 ms and this repeats until complete execution of MyThread.
注意1:這里,我們已經編寫了/*mt.join(1000)*/,表示當前正在執行的線程[main]將給另一個名為[ MyThread mt ]的線程一個機會,持續1000 ms,然后在主線程執行之后,有機會執行如果主線程進入等待狀態或睡眠模式,則MyThread再次有機會獲得1000毫秒的機會,并重復執行直到MyThread完全執行為止。
Output
輸出量
E:\Programs>javac MainThread1.java
E:\Programs>java MainThread1
Thread started:Thread-0
Thread started:Thread-0
Thread started:main
Thread started:main
Thread ended:main
Thread started:Thread-0
Thread started:Thread-0
Thread started:Thread-0
Thread Ended :Thread-0
翻譯自: https://www.includehelp.com/java/thread-class-final-synchronized-void-join-long-time_in_ms-method-with-example.aspx