xxxxxxxxxx
class Gfg1 {
public static void main(String args[]) {
// delimiter is "<" and elements are "Four", "Five", "Six", "Seven"
String gfg1 = String.join(" < ", "Four", "Five", "Six", "Seven");
System.out.println(gfg1);
}
}
//Four < Five < Six < Seven
xxxxxxxxxx
A thread can invoke the join() method on other thread to wait for other thread to complete its
execution. Assume we have two threads, t1 and t2 threads .
A running thread t1 invokes join() on thread t2 then t1 thread will wait in to waiting state until t2 completes.
Once t2 completes the execution, t1 will continue.
join() method throws Interrupted Exception so when ever we use join() method
we should handle Interrrupted Exception by throws or by using try catch block.
xxxxxxxxxx
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
package Edureka;
import java.io.*;
import java.util.*;
public class Threadjoiningmethod extends Thread{
public void run(){
for(int i=1;i<=4;i++){
try{
Thread.sleep(500);
}catch(Exception e){System.out.println(e);}
System.out.println(i);
}
}
public static void main(String args[]){
Threadjoiningmethod th1=new Threadjoiningmethod ();
Threadjoiningmethod th2=new Threadjoiningmethod ();
Threadjoiningmethod th3=new Threadjoiningmethod ();
th1.start();
try{
th1.join();
}
catch(Exception e){
System.out.println(e);
}
th2.start();
th3.start();
}
}
xxxxxxxxxx
String.join(" - ", "This", "course", "is", "awesome");
String.valueOf(22); // retourne "22"