Most of the content is based on official documents and online experience summary, through personal practice to organize and accumulate, only for reference.
The join () method is an instance method of the Java.lang.Thread class, and the method signature
Public final void Join () throws Interruptedexception
Public final void Join (Long Millis) throws Interruptedexception
Public final void Join (long millis, int nanos) throws Interruptedexception
The join () method causes one thread to execute at the end of another thread, and if invoked on one thread instance, the currently running thread will block until the thread instance calling the join () method ends
The join () method receives a long type parameter that causes the thread to fail after a specific millisecond is waiting
The join () method can also receive an extra int type parameter
Example:
public static void Main (string[] args) throws interruptedexception {
thread t1 = new Thread (new Runnable () {
@Over Ride public
Void Run () {
System.out.println ("A-thread started!");
System.out.println ("Ready to sleep 5 seconds!");
try {
Thread.Sleep (5000);
} catch (Interruptedexception e) {
e.printstacktrace ();
}
System.out.println ("ended!");
Thread t2 = new Thread (new Runnable () {
@Override public
void Run () {
System.out.println ("Second Thread St Arted! ");
System.out.println ("Second thread ended!");
}
);
T1.start ();
T1.join ();
T2.start ();
}
If T1.join () is commented out, run the result:
To run the result after removing the annotation:
Pass a long type parameter to the join () method so that the maximum waits for 3 seconds
public static void Main (string[] args) throws interruptedexception {
thread t1 = new Thread (new Runnable () {
@Over Ride public
Void Run () {
System.out.println ("A-thread started!");
System.out.println ("Ready to sleep 5 seconds!");
try {
Thread.Sleep (5000);
} catch (Interruptedexception e) {
e.printstacktrace ();
}
System.out.println ("ended!");
Thread t2 = new Thread (new Runnable () {
@Override public
void Run () {
System.out.println ("Second Thread St Arted! ");
System.out.println ("Second thread ended!");
}
);
T1.start ();
T1.join (3000);
T2.start ();
}
Run Result: