Skip to content

Commit 14acb1b

Browse files
committed
Java Thread 예시 코드 추가
1 parent 4c90609 commit 14acb1b

File tree

4 files changed

+56
-26
lines changed

4 files changed

+56
-26
lines changed
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,16 @@
11
package io.concurrency.chapter02.exam01;
22

33
public class ExtendThreadExample {
4-
public static void main(String[] args) {
4+
public static void main(String[] args) {
55

6-
MyThread myThread = new MyThread();
7-
myThread.start();
8-
}
9-
}
6+
MyThread myThread = new MyThread();
7+
myThread.start();
8+
}
9+
}
1010

11-
class MyThread extends Thread{
12-
@Override
13-
public void run() {
14-
System.out.println(Thread.currentThread().getName() + " :스레드 실행 중.. ");
15-
}
16-
}
11+
class MyThread extends Thread {
12+
@Override
13+
public void run() {
14+
System.out.println(Thread.currentThread().getName() + " :스레드 실행 중.. ");
15+
}
16+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
package io.concurrency.chapter02.exam01;
2+
3+
public class ExtendedThread extends Thread {
4+
5+
public void run() {
6+
for (int i = 0; i < 5; i++) {
7+
System.out.println(String.format("%s Value %d", Thread.currentThread().getId(), i));
8+
}
9+
}
10+
11+
public static void main(String[] args) {
12+
ExtendedThread thread = new ExtendedThread();
13+
thread.start();
14+
}
15+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
package io.concurrency.chapter02.exam01;
2+
3+
class RunnableImpl implements Runnable {
4+
5+
public void run() {
6+
for (int i = 0; i < 5; i++) {
7+
System.out.println(String.format("%s Value %d", Thread.currentThread().getId(), i));
8+
}
9+
}
10+
11+
public static void main(String[] args) {
12+
Thread thread = new Thread(new RunnableImpl());
13+
thread.start();
14+
}
15+
}
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,25 @@
11
package io.concurrency.chapter03.exam01;
22

33
public class InterruptSleepExample {
4-
public static void main(String[] args) throws InterruptedException {
4+
public static void main(String[] args) throws InterruptedException {
55

6-
Thread sleepingThread = new Thread(() -> {
7-
try {
8-
System.out.println("20초 동안 잠듭니다. 인터럽트되지 않는다면 계속 잠들어 있을 것입니다.");
6+
Thread sleepingThread = new Thread(() -> {
7+
try {
8+
System.out.println("20초 동안 잠듭니다. 인터럽트되지 않는다면 계속 잠들어 있을 것입니다.");
99

10-
Thread.sleep(20000); // 스레드는 지정된 시간 동안 잠듭니다
10+
Thread.sleep(20000); // 스레드는 지정된 시간 동안 잠듭니다
1111

12-
System.out.println("인터럽트 없이 잠에서 깨었습니다.");
12+
System.out.println("인터럽트 없이 잠에서 깨었습니다.");
1313

14-
} catch (InterruptedException e) {
15-
System.out.println("잠들어 있는 동안 인터럽트 되었습니다!");
16-
}
17-
});
14+
} catch (InterruptedException e) {
15+
System.out.println("잠들어 있는 동안 인터럽트 되었습니다!");
16+
}
17+
});
1818

19-
sleepingThread.start();
19+
sleepingThread.start();
2020

21-
Thread.sleep(1000);
22-
//
23-
sleepingThread.interrupt();
24-
}
21+
Thread.sleep(1000);
22+
23+
sleepingThread.interrupt();
24+
}
2525
}

0 commit comments

Comments
 (0)