조인 기능
이 함수는 스레드 실행의 시작과 다른 스레드 실행의 끝을 결합하는 데 사용됩니다. 이렇게 하면 두 번째 스레드가 실행을 중지할 때까지 첫 번째 스레드가 실행되지 않습니다. 이 함수는 스레드가 종료될 때까지 특정 시간(밀리초) 동안 기다립니다.
예를 들어 보겠습니다 -
예시
import java.lang.*;
public class Demo implements Runnable{
public void run(){
Thread my_t = Thread.currentThread();
System.out.println("The name of the current thread is " + my_t.getName());
System.out.println("Is the current thread alive? " + my_t.isAlive());
}
public static void main(String args[]) throws Exception{
Thread my_t = new Thread(new Demo());
System.out.println("The instance has been created and started");
my_t.start();
my_t.join(30);
System.out.println("The threads will be joined after 30 milli seconds");
System.out.println("The name of the current thread is " + my_t.getName());
System.out.println("Is the current thread alive? " + my_t.isAlive());
}
} 출력
The instance has been created and started The threads will be joined after 30 milli seconds The name of the current thread is Thread-0 The name of the current thread is Thread-0 Is the current thread alive? true Is the current thread alive? true
Demo라는 클래스는 Runnable 클래스를 구현합니다. 현재 스레드를 새로 생성된 스레드로 할당하는 '실행' 기능이 정의되어 있습니다. 메인 함수에서 스레드의 새 인스턴스가 생성되고 'start' 함수를 사용하여 시작됩니다. 이 스레드는 특정 시간 후에 다른 스레드와 결합됩니다. 관련 메시지가 화면에 표시됩니다.