Skip to content

Commit a18ce8a

Browse files
ramansahasimaibin
authored andcommitted
Bael 1427 Life cycle of thread (eugenp#3637)
* Interrupted thread before logging * abbrevated word thread and interrupted main thread
1 parent 97f56f3 commit a18ce8a

6 files changed

Lines changed: 135 additions & 0 deletions

File tree

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
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+
}
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
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+
}
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
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+
}
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
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+
}
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
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+
}
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
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+
}

0 commit comments

Comments
 (0)