File tree Expand file tree Collapse file tree
core-java-concurrency/src/main/java/com/baeldung/concurrent/threadlifecycle Expand file tree Collapse file tree Original file line number Diff line number Diff line change 1+ package com .baeldung .concurrent .threadlifecycle ;
2+
3+ public class BlockedState {
4+ public static void main (String [] args ) throws InterruptedException {
5+ Thread t1 = new Thread (new DemoThreadB ());
6+ Thread t2 = new Thread (new DemoThreadB ());
7+
8+ t1 .start ();
9+ t2 .start ();
10+
11+ Thread .sleep (1000 );
12+
13+ System .out .println (t2 .getState ());
14+ System .exit (0 );
15+ }
16+ }
17+
18+ class DemoThreadB implements Runnable {
19+ @ Override
20+ public void run () {
21+ commonResource ();
22+ }
23+
24+ public static synchronized void commonResource () {
25+ while (true ) {
26+ // Infinite loop to mimic heavy processing
27+ // Thread 't1' won't leave this method
28+ // when Thread 't2' enters this
29+ }
30+ }
31+ }
Original file line number Diff line number Diff line change 1+ package com .baeldung .concurrent .threadlifecycle ;
2+
3+ public class NewState implements Runnable {
4+ public static void main (String [] args ) {
5+ Runnable runnable = new NewState ();
6+ Thread t = new Thread (runnable );
7+ System .out .println (t .getState ());
8+ }
9+
10+ @ Override
11+ public void run () {
12+
13+ }
14+ }
Original file line number Diff line number Diff line change 1+ package com .baeldung .concurrent .threadlifecycle ;
2+
3+ public class RunnableState implements Runnable {
4+ public static void main (String [] args ) {
5+ Runnable runnable = new NewState ();
6+ Thread t = new Thread (runnable );
7+ t .start ();
8+ System .out .println (t .getState ());
9+ }
10+
11+ @ Override
12+ public void run () {
13+
14+ }
15+ }
Original file line number Diff line number Diff line change 1+ package com .baeldung .concurrent .threadlifecycle ;
2+
3+ public class TerminatedState implements Runnable {
4+ public static void main (String [] args ) throws InterruptedException {
5+ Thread t1 = new Thread (new TerminatedState ());
6+ t1 .start ();
7+ Thread .sleep (1000 );
8+ System .out .println (t1 .getState ());
9+ }
10+
11+ @ Override
12+ public void run () {
13+ // No processing in this block
14+ }
15+ }
Original file line number Diff line number Diff line change 1+ package com .baeldung .concurrent .threadlifecycle ;
2+
3+ public class TimedWaitingState {
4+ public static void main (String [] args ) throws InterruptedException {
5+ DemoThread obj1 = new DemoThread ();
6+ Thread t1 = new Thread (obj1 );
7+ t1 .start ();
8+ // The following sleep will give enough time for ThreadScheduler
9+ // to start processing of thread t1
10+ Thread .sleep (1000 );
11+ System .out .println (t1 .getState ());
12+ }
13+ }
14+
15+ class DemoThread implements Runnable {
16+ @ Override
17+ public void run () {
18+ try {
19+ Thread .sleep (5000 );
20+ } catch (InterruptedException e ) {
21+ Thread .currentThread ().interrupt ();
22+ e .printStackTrace ();
23+ }
24+ }
25+ }
Original file line number Diff line number Diff line change 1+ package com .baeldung .concurrent .threadlifecycle ;
2+
3+ public class WaitingState implements Runnable {
4+ public static Thread t1 ;
5+
6+ public static void main (String [] args ) {
7+ t1 = new Thread (new WaitingState ());
8+ t1 .start ();
9+ }
10+
11+ public void run () {
12+ Thread t2 = new Thread (new DemoThreadWS ());
13+ t2 .start ();
14+
15+ try {
16+ t2 .join ();
17+ } catch (InterruptedException e ) {
18+ Thread .currentThread ().interrupt ();
19+ e .printStackTrace ();
20+ }
21+ }
22+ }
23+
24+ class DemoThreadWS implements Runnable {
25+ public void run () {
26+ try {
27+ Thread .sleep (1000 );
28+ } catch (InterruptedException e ) {
29+ Thread .currentThread ().interrupt ();
30+ e .printStackTrace ();
31+ }
32+
33+ System .out .println (WaitingState .t1 .getState ());
34+ }
35+ }
You can’t perform that action at this time.
0 commit comments