join : Thread가 종료될 때까지 기다리게 하는 method
즉 Thread가 종료된 후에 다음 로직을 수행해야 하는 경우에 사용하는 method가 join


이전 예제에서 main method가 Thread가 종료되기 전에 종료됨.
Thread가 종료된 후에 main method가 종료시켜야 하는 경우 사용하는 방법

ThreadExam01.java

public class ThreadExam01 extends Thread {
	int seq;
	
	public ThreadExam01(int seq){
		this.seq = seq;
	}
	
	@Override
	public void run() {
		System.out.println(this.seq + " thread START");
		
		try{
			Thread.sleep(1000);
		}catch(Exception ex){
			ex.printStackTrace();
		}
		
		System.out.println(this.seq + " thread END");
	}
}
ThreadMain01.java
public class ThreadMain01 {

	public static void main(String[] args) {
		List threadList = new ArrayList<>();
		
		try{
			for(int i=0; i<5; i++){
				Thread thread = new ThreadExam01(i);
				thread.start();
				
				threadList.add(thread);
			}
			
			for(int i=0; i<'threadList.size()'; i++){
				Thread th = threadList.get(i);
				th.join();
			}
		}catch(Exception ex){
			ex.printStackTrace();
		}			
		
		System.out.println("ThreadMain01 END...");
	}
}
결과
1 thread START
4 thread START
0 thread START
3 thread START
2 thread START
3 thread END
4 thread END
2 thread END
0 thread END
1 thread END
ThreadMain01 END...

ThreadExam02.java
public class ThreadExam02 implements Runnable {
	int seq;
	
	public ThreadExam02(int seq) {
		this.seq = seq;
	}
	
	@Override
	public void run() {
		System.out.println(this.seq + " thread START");
		
		try{
			Thread.sleep(2000);
		}catch(Exception ex){
			ex.printStackTrace();
		}
		
		System.out.println(this.seq + " thread END");
	}
}
ThreadMain02.java
public class ThreadMain02 {

	public static void main(String[] args) {
		List threadList = new ArrayList<>();
		
		try{
			for(int i=0; i<5; i++){
				Thread thread = new Thread(new ThreadExam02(i));
				thread.start();
				
				threadList.add(thread);
			}
			
			for(int i=0; i<'threadList.size()'; i++){
				Thread th = threadList.get(i);
				th.join();
			}
			
			System.out.println("ThreadMain02 END");
		}catch(Exception ex){
			ex.printStackTrace();
		}
	}
}
결과
1 thread START
0 thread START
3 thread START
2 thread START
4 thread START
3 thread END
0 thread END
4 thread END
2 thread END
1 thread END
ThreadMain02 END

ThreadMain03.java
Main class
public class ThreadMain03 {
	
	public static void main(String[] args) {
		System.out.println("ThreadMain03 START");

		try{
			for(int i=0; i<5; i++){
				Thread exam03 = new ThreadExam03((i+1) + "번째");
				exam03.start();
				exam03.join();	//쓰레드가 종료될 때까지 대기
			}
		}catch(Exception ex){
			ex.printStackTrace();
		}
		
		System.out.println("ThreadMain03 END");
	}
}


//ThreadExam03 Thread class
class ThreadExam03 extends Thread {
	String threadName;
	
	public ThreadExam03(String name) {
		this.threadName = name;
	}
	
	@Override
	public void run() {
		System.out.println(this.threadName + " START");
		
		try{
			Thread.sleep(2000);
		}catch(Exception ex){
			ex.printStackTrace();
		}
		
		System.out.println(this.threadName + " END");
	}
}
결과
ThreadMain03 START
1번째 START
1번째 END
2번째 START
2번째 END
3번째 START
3번째 END
4번째 START
4번째 END
5번째 START
5번째 END
ThreadMain03 END

+ Recent posts