_thread_in_vm
线程类最终无效连接(long time_in_ms,int time_in_ns) (Thread Class final void join(long time_in_ms, int time_in_ns))
This method is available in package java.lang.Thread.join(long time_in_ms, int time_in_ns).
软件包java.lang.Thread.join(long time_in_ms,int time_in_ns)中提供了此方法。
join(long time_in_ms, int time_in_ns) method is applicable when currently executing thread wants to wait for a particular amount of time in milliseconds with additional time also in nanoseconds until completing some other thread then we should go for join(long time_in_ms, int time_in_ns) method of Thread class.
join(long time_in_ms,int time_in_ns)方法适用于当前正在执行的线程想要以毫秒为单位的特定时间量,另外的时间也以纳秒为单位,直到完成其他线程为止,然后我们应该进行join(long time_in_ms,int time_in_ns) 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 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, 500) then thread t1 will entered into waiting state for 1000 milliseconds + 500 nanoseconds until t2 completes and suppose in case if t2 couldn't complete its execution in 1000 ms + 500 ns 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 + 500 ns and same process will repeat.
假设如果线程t1执行了t2.join(1000,500),则线程t1将进入等待状态1000毫秒+ 500纳秒,直到t2完成,并假设t2无法在1000 ms + 500内完成其执行ns,因此在这种情况下, t1将有机会执行,并且如果线程t1进入等待状态或睡眠模式,则t2将再次有机会在1000 ms + 500 ns内执行其执行,并且将重复相同的过程。
Syntax:
句法:
final void join(long time_in_ms, int time_in_ns){
}
Parameter(s):
参数:
When we write t2.join(2000, 1000), so this line means currently executing thread will stop its execution for 2000 milliseconds +1000 nanoseconds until t2 completion.
当我们编写t2.join(2000,1000)时 ,此行表示当前正在执行的线程将在2000毫秒+1000纳秒内停止执行,直到t2完成。
Return value:
返回值:
The return type of this method is void, it does not return anything.
此方法的返回类型为void ,它不返回任何内容。
Java程序演示join(long time_in_ms,int time_in_ns)方法的示例 (Java program to demonstrate example of join(long time_in_ms, int time_in_ns) 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, 500);
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,500)*/ means currently executing thread [main] will give a chance to another thread named [MyThread mt] for 1000 ms +500 ns 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 +500 ns and this repeats until complete execution of MyThread.
注意1:这里我们写了/*mt.join(1000,500)*/,表示当前正在执行的线程[main]将给另一个名为[ MyThread mt ]的线程一个机会,持续1000 ms +500 ns,然后在主线程获得有机会执行,并且如果主线程进入等待状态或睡眠模式,则MyThread再次有机会获得1000 ms +500 ns的机会,并重复执行直到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 started:Thread-0
Thread ended:main
Thread started:Thread-0
Thread started:Thread-0
Thread Ended :Thread-0
_thread_in_vm