-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathThreads.java
More file actions
36 lines (30 loc) · 1.01 KB
/
Threads.java
File metadata and controls
36 lines (30 loc) · 1.01 KB
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
package org.example.thread;
/**
* @Author : yion
* @Date : 2017. 5. 21.
* @Description :
*/
public class Threads {
public static void main(String[] args) throws InterruptedException {
// 새로운 스레드를 생성 할 때는 새로운 Thread 객체를 생성해야 한다.
final Thread separateThread = new Thread(new ThreadPrinter());
separateThread.start();
for (int i = 0; i < 5; i++) {
System.out.println("From the main Thread : " + Thread.currentThread().getName());
Thread.sleep(1000);
}
}
private static class ThreadPrinter implements Runnable {
@Override
public void run() {
for (int i = 0; i < 5; i++) {
System.out.println("From the new thread : " + Thread.currentThread().getName());
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}
}