Home > other >  The join method is internal actual wait, can't understand is, in a thread b thread calls the jo
The join method is internal actual wait, can't understand is, in a thread b thread calls the jo

Time:02-13


The join method is internal actual wait, can't understand is, in a thread b thread calls the join method, why has been waiting until the end of the thread b is a thread,

CodePudding user response:


Brother, you at least code posted, or how to find problems

CodePudding user response:


This logic you describe chaos ah, what is Thread_A. Join or Thead_B. Join
Take a look at https://docs.microsoft.com/zh-cn/dotnet/api/system.threading.thread.join? Redirectedfrom=MSDN& View=netframework 4.8 # System_Threading_Thread_Join
Inside the
Annotations to write very clear,

CodePudding user response:

The main thread calls another thread object the join method, why call main will wait (0).

Public class ThreadTest {
Public static void main (String [] args) {
Thread01 t1=new Thread01 ();
T1. Start ();
Try {
T1. The join ();//in the main thread calls the t1 thread of the join () method
} catch InterruptedException (e) {}
for(int i=0; i <10; I++) {
System. The out. Println (" in the main ");
Try {
Thread. CurrentThread (). Sleep (1000);
} catch InterruptedException (e) {}
}
}
}
The class Thread01 extends Thread {
Public void the run () {
for(int i=0; i <10; I++) {
System. The out. Println (" in "+ Thread. The currentThread (). The getName () +" threads "+" I="+ I);
Try {
Thread. CurrentThread (). Sleep (1000);
} catch InterruptedException (e) {}
}
}
}

1 in the main thread calls the t1 threads of the join () method

And in the JDK:

Public final void the join () throws InterruptedException {
The join (0);
}

Public final synchronized void join (long millis)
Throws InterruptedException {
Long base=System. CurrentTimeMillis ();
Long=0;

If (millis & lt; 0 {
Throw new IllegalArgumentException (" the timeout value is negative, ");
}

If (millis==0) {
While (isAlive ()) {
Wait (0);
}
} else {
While (isAlive ()) {
Long delay=millis - now;
If (delay & lt;=0) {
break;
}
Wait (delay);
Now=System. CurrentTimeMillis () - base;
}
}
}

Apparently call the wait () method, let the waiting thread object, see here should be a thread object is arguably: t1 to wait, how to become the main thread waits for t1 is done?
  • Related