Thread를 생성하는 방법 중에서도 Thread 클래스로 부터 직접 생성하는 방법


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
31
32
33
34
35
36
public class CreateThread {
    
    /*
        Thread 클래스로부터 Thread 직접 생성
    */
 
    // 기본적으로 thread를 생성하기 위해서는 Runnable 타입의 인자가 필요
    
 
    //thread 생성 방법1. Runnble 구현 클래스를 통한 thread 생성 
    static class Task implements Runnable {
        public void run() {
            System.out.println("hello thread");
        }
    }
 
    public static void main(String[] args) {
        Task task = new Task();
        Thread thread1 = new Thread(task);
        thread1.start();
        
        //thread 생성방법2. 익명 구현 객체를 통한 thread 생성
        Thread thread2 = new Thread(new Runnable() {
            public void run() {
                System.out.println("thread2 created by Anonymous class");
            }
        });
        thread2.start();
        
        //thread 생성방법3. 람다식을 이용한 thread 생성
        Thread thread3 = new Thread( () -> {
            System.out.println("thread3 created by lambda");
        });
        thread3.start();
    }
}
cs


'Java' 카테고리의 다른 글

[제너릭]와일드카드 타입  (0) 2017.02.17
[제너릭]제너릭 메소드  (0) 2017.02.16
[제너릭] 제너릭과 비제너릭 비교  (0) 2017.02.15
Thread 상속으로 thread 생성  (0) 2017.01.27
멀티 스레드 개념  (0) 2017.01.25

+ Recent posts