donaricano-btn

Joining a thread

- join() waits for a thread to die


1. Syntax

- public void join()throws InterruptedException

- public void join(long milliseconds)throws InterruptedException


2. Example

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
public class join extends Thread{
    public void run(){
        for(int i = 0 ; i < 5; i++){
            try{
                Thread.sleep(500);
            }catch(Exception e){
                System.out.println(e);
            }
            System.out.println(i);
        }
         
    }
    public static void main(String[]args){
        join t1 = new join();
        join t2 = new join();
        join t3 = new join();
         
        t1.start();
         
        try{
            t1.join();
        }catch(Exception e){
            System.out.println(e);
        }
         
        t2.start();
        t3.start();
    }
}


'Back-End > Java_1' 카테고리의 다른 글

[Java] Thread Priority  (0) 2016.10.07
[Java] Naming Thread  (0) 2016.10.07
[Java] Call a run() directly instead start()  (0) 2016.10.07
[Java] Sleeping a thread  (0) 2016.10.07
[Java] Thread Scheduler  (0) 2016.10.07
블로그 이미지

리딩리드

,