File tree Expand file tree Collapse file tree 6 files changed +108
-0
lines changed
src/main/java/io/concurrency/chapter02/exam03 Expand file tree Collapse file tree 6 files changed +108
-0
lines changed Original file line number Diff line number Diff line change
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
+ }
Original file line number Diff line number Diff line change
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
+ }
Original file line number Diff line number Diff line change
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
+ }
Original file line number Diff line number Diff line change
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
+ }
Original file line number Diff line number Diff line change
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
+ }
Original file line number Diff line number Diff line change
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
+ }
You can’t perform that action at this time.
0 commit comments