Skip to content

Commit 9cd1a89

Browse files
committed
lecture
1 parent 9c476bd commit 9cd1a89

File tree

6 files changed

+108
-0
lines changed

6 files changed

+108
-0
lines changed
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
package io.concurrency.chapter02.exam03;
2+
3+
public class BlockedStateThreadExample {
4+
5+
public static void main(String[] args) throws InterruptedException {
6+
final Object lock = new Object();
7+
Thread thread1 = new Thread(() -> {
8+
synchronized (lock) {
9+
while (true) {
10+
// 무한 루프로 lock을 계속 점유
11+
}
12+
}
13+
});
14+
Thread thread2 = new Thread(() -> {
15+
synchronized (lock) {
16+
System.out.println("Thread 2 실행 중");
17+
}
18+
});
19+
thread1.start();
20+
Thread.sleep(100); // thread1이 lock을 점유하도록 잠시 대기
21+
thread2.start();
22+
Thread.sleep(100); // thread2가 lock을 기다리는 상태로 대기
23+
System.out.println("스레드 2 상태: " + thread2.getState()); // BLOCKED
24+
}
25+
26+
}
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
package io.concurrency.chapter02.exam03;
2+
3+
public class NewStateThreadExample {
4+
5+
public static void main(String[] args) {
6+
Thread thread = new Thread(() -> {
7+
System.out.println("스레드 실행 중");
8+
});
9+
System.out.println("스레드 상태: " + thread.getState()); // NEW
10+
}
11+
12+
}
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
package io.concurrency.chapter02.exam03;
2+
3+
public class RunnableStateThreadExample {
4+
5+
public static void main(String[] args) throws InterruptedException {
6+
7+
Thread thread = new Thread(() -> {
8+
while (true) {
9+
for (int i = 0; i < 1000000000; i++) {
10+
if(i%1000000000 == 0){
11+
System.out.println("스레드 상태: " + Thread.currentThread().getState()); // RUNNABLE
12+
}
13+
}
14+
}
15+
});
16+
thread.start();
17+
}
18+
}
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
package io.concurrency.chapter02.exam03;
2+
3+
public class TerminatedStateThreadExample {
4+
5+
public static void main(String[] args) throws InterruptedException {
6+
Thread thread = new Thread(() -> {
7+
System.out.println("스레드 실행 중");
8+
});
9+
thread.start();
10+
thread.join(); // 스레드가 종료될 때까지 기다림
11+
System.out.println("스레드 상태: " + thread.getState()); // TERMINATED
12+
}
13+
14+
}
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
package io.concurrency.chapter02.exam03;
2+
3+
public class TimedWaitingStateThreadExample {
4+
5+
public static void main(String[] args) throws InterruptedException {
6+
Thread thread = new Thread(() -> {
7+
try {
8+
Thread.sleep(10000);
9+
} catch (InterruptedException e) {
10+
e.printStackTrace();
11+
}
12+
});
13+
thread.start();
14+
Thread.sleep(100);
15+
System.out.println("스레드 상태: " + thread.getState()); // TIMED_WAITING
16+
}
17+
18+
}
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
package io.concurrency.chapter02.exam03;
2+
3+
public class WaitingStateThreadExample {
4+
5+
public static void main(String[] args) throws InterruptedException {
6+
final Object lock = new Object();
7+
Thread thread = new Thread(() -> {
8+
synchronized (lock) {
9+
try {
10+
lock.wait();
11+
} catch (InterruptedException e) {
12+
e.printStackTrace();
13+
}
14+
}
15+
});
16+
thread.start();
17+
Thread.sleep(100);
18+
System.out.println("스레드 상태: " + thread.getState()); // WAITING
19+
}
20+
}

0 commit comments

Comments
 (0)