join()방법의 역할

986 단어 Java
             run()   ,   join()       。
 ,       ,       。

  ,      A join()       A   ,      A join(2000)  
     A   ,     2s,    :
public class JoinTest() {
	public static void main(String[] args) {
		Thread t = new Thread(new ThreadImp());
		t.start();
		try {
			t.join(1000);  //     t  ,  1s
			if(t.isAlive()) {
				System.out,pringtlin("t has not finished");
			} else {
				System.out,pringtlin("t has finished");}
			}
			System.out,pringtlin("t finished");
		} catch(InterruptedException e) {
			e.printStackTrace();
		}
	}
}

class ThreadImp implements Runnable() {
	public void run() {
		try{
			System.out,pringtlin("Begin ThreadImp");
			Thread.sleep(5000);
			System.out,pringtlin("End TheadImp");
		} catch(InterruptedException e) {
			e.printStackTrace();
		}
	}
}

프로그램의 실행 결 과 는:
	Begin ThreadImp
	t had not finished
	joinFinished
	End ThreadImp

좋은 웹페이지 즐겨찾기