- 전체
- JAVA 일반
- JAVA 수학
- JAVA 그래픽
- JAVA 자료구조
- JAVA 인공지능
- JAVA 인터넷
- Java Framework
- Java GUI (AWT,SWING,SWT,JFACE)
- SWT and RCP (web RAP/RWT)[eclipse], EMF
JAVA 일반 [java 일반] Runnable을 이용한 Thread 생성 방법들
2024.05.01 16:46
[java 일반] Runnable을 이용한 Thread 생성 방법들
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 |
출처: https://eddyplusit.tistory.com/28 [EddIT:티스토리]
본 웹사이트는 광고를 포함하고 있습니다.
광고 클릭에서 발생하는 수익금은 모두 웹사이트 서버의 유지 및 관리, 그리고 기술 콘텐츠 향상을 위해 쓰여집니다.
광고 클릭에서 발생하는 수익금은 모두 웹사이트 서버의 유지 및 관리, 그리고 기술 콘텐츠 향상을 위해 쓰여집니다.

