Join() method: Thread provides a thread to wait for another thread to complete. When a join method (t2.join()) of another thread (such as a t2 thread) is called in a program execution flow (such as the main thread), the calling thread (main thread) will be blocked until it is added by the join() method. The join thread (t2.start()) is executed.
public class MyJoinTest implements Runnable {
public void run() {
for (int i = 0; i < 5; i++) {
System.out.println(Thread.currentThread().getName() + "====" + i);
}
}
public static void main(String[] args) throws InterruptedException {
MyJoinTest myJoinTest = new MyJoinTest();
Thread t1 = new Thread(myJoinTest, "normal thread");
Thread t2 = new Thread(myJoinTest, "thread of join");
T1.start(); //Start t1 thread
/ / Below belongs to the main main thread
for (int i = 0; i < 10; i++) {
if (i == 5) {
T2.start(); //Start t2 thread
}
System.out.println(Thread.currentThread().getName() + "====" + i);
}
}
}
The output is
main====0
main====1
main====2
main====3
main====4
normal thread ====0
normal thread ====1
normal thread ====2
Ordinary thread ====3
Normal Thread ====4
main====5
main====6
main====7
main====8
main====9
join thread ====0
join thread ====1
join thread ====2
join thread ====3
join thread ====4
public class MyJoinTest implements Runnable {
public void run() {
for (int i = 0; i < 5; i++) {
System.out.println(Thread.currentThread().getName() + "====" + i);
}
}
public static void main(String[] args) throws InterruptedException {
MyJoinTest myJoinTest = new MyJoinTest();
Thread t1 = new Thread(myJoinTest, "normal thread");
Thread t2 = new Thread(myJoinTest, "thread of join");
T1.start(); //Start t1 thread
/ / Below belongs to the main main thread
for (int i = 0; i < 10; i++) {
if (i == 5) {
T2.start(); //Start t2 thread
//The main thread calls the join method of the t2 thread, causing the main thread to wait for the end of t2 execution before it can execute downward.
t2.join();
}
System.out.println(Thread.currentThread().getName() + "====" + i);
}
}
}
The output is
main====0
main====1
main====2
main====3
main====4
normal thread ====0
normal thread ====1
normal thread ====2
Ordinary thread ====3
Normal Thread ====4
join thread ====0
join thread ====1
join thread ====2
join thread ====3
join thread ====4
main====5
main====6
main====7
main====8
main====9
public final synchronized void join(long millis)
throws InterruptedException {
long base = System.currentTimeMillis();
long now = 0;
if (millis < 0) {
throw new IllegalArgumentException("timeout value is negative");
}
if (millis == 0) {
while (isAlive()) {
wait(0);
}
} else {
while (isAlive()) {
long delay = millis - now;
if (delay <= 0) {
break;
}
wait(delay);
now = System.currentTimeMillis() - base;
}
}
}
From the source code, you can see that the principle of the join method is to call the wait method of the corresponding thread to wait for operation. For example, the join method of the B thread is called in the A thread, which is equivalent to calling the wait method of the B thread in the A thread. When the B thread finishes executing (or arrives at the waiting time), the B thread will automatically call its own notifyAll method to wake up the A thread, thus achieving the purpose of synchronization.
Introduction to join The join() method is a method in the Thread class whose definition is to wait for the thread to terminate. In fact, the join() method will suspend the execution of the calling thr...
java.lang.Threadofjoin()Methods Source follows: The first method signature is _synchronized_。 Holds the current thread (ta) Object lock thread (tb) Callingta.join()Methods thread _tb_ will perform at ...
Multithreading join (), and there can be no-argument argument, join (long mills) .join is to provide a method of Thread, join the main method used to achieve this thread when death is the beginning of...
Disclaimer: This article is CSDN blogger "2 to the power of 32" in the original article, follow the CC 4.0 by-sa copyright agreement, reproduced, please attach the original source link and t...
Role 1.join () of When you call a thread's join () method, which suspends the calling thread until the end of the execution of the calling thread, the calling thread will continue to execute. 2. Put j...
xl_echo editing and finishing, welcome to reprint, please declare the source of the article. For more IT, programming cases, and materials, please contact QQ: 1280023003, Jiaqun 298140694. Undefeated ...
The role of the method join () is to wait for the thread object to be destroyed. The function of the method join () is that the thread object x belongs to normally executes the task in the run () meth...
The role of join is to let other threads become waiting, t1.join();// Let other threads become waiting, and not release until the current t1 thread finishes executing. thread.Join adds the specified t...
In many cases, the main thread creates and starts a child thread. If a large amount of time-consuming operations are performed in the child thread, the main thread will end before the child thread. At...
As for whether the "lock ID" will be released by the join() method, it can be considered that, under the premise of whether there is a synchronized keyword, it can be roughly divided into fo...