Bael 1427 Life cycle of thread (#3637)
* Interrupted thread before logging * abbrevated word thread and interrupted main thread
This commit is contained in:
parent
97f56f33f6
commit
a18ce8a380
@ -0,0 +1,31 @@
|
|||||||
|
package com.baeldung.concurrent.threadlifecycle;
|
||||||
|
|
||||||
|
public class BlockedState {
|
||||||
|
public static void main(String[] args) throws InterruptedException {
|
||||||
|
Thread t1 = new Thread(new DemoThreadB());
|
||||||
|
Thread t2 = new Thread(new DemoThreadB());
|
||||||
|
|
||||||
|
t1.start();
|
||||||
|
t2.start();
|
||||||
|
|
||||||
|
Thread.sleep(1000);
|
||||||
|
|
||||||
|
System.out.println(t2.getState());
|
||||||
|
System.exit(0);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
class DemoThreadB implements Runnable {
|
||||||
|
@Override
|
||||||
|
public void run() {
|
||||||
|
commonResource();
|
||||||
|
}
|
||||||
|
|
||||||
|
public static synchronized void commonResource() {
|
||||||
|
while(true) {
|
||||||
|
// Infinite loop to mimic heavy processing
|
||||||
|
// Thread 't1' won't leave this method
|
||||||
|
// when Thread 't2' enters this
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
@ -0,0 +1,14 @@
|
|||||||
|
package com.baeldung.concurrent.threadlifecycle;
|
||||||
|
|
||||||
|
public class NewState implements Runnable {
|
||||||
|
public static void main(String[] args) {
|
||||||
|
Runnable runnable = new NewState();
|
||||||
|
Thread t = new Thread(runnable);
|
||||||
|
System.out.println(t.getState());
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void run() {
|
||||||
|
|
||||||
|
}
|
||||||
|
}
|
@ -0,0 +1,15 @@
|
|||||||
|
package com.baeldung.concurrent.threadlifecycle;
|
||||||
|
|
||||||
|
public class RunnableState implements Runnable {
|
||||||
|
public static void main(String[] args) {
|
||||||
|
Runnable runnable = new NewState();
|
||||||
|
Thread t = new Thread(runnable);
|
||||||
|
t.start();
|
||||||
|
System.out.println(t.getState());
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void run() {
|
||||||
|
|
||||||
|
}
|
||||||
|
}
|
@ -0,0 +1,15 @@
|
|||||||
|
package com.baeldung.concurrent.threadlifecycle;
|
||||||
|
|
||||||
|
public class TerminatedState implements Runnable {
|
||||||
|
public static void main(String[] args) throws InterruptedException {
|
||||||
|
Thread t1 = new Thread(new TerminatedState());
|
||||||
|
t1.start();
|
||||||
|
Thread.sleep(1000);
|
||||||
|
System.out.println(t1.getState());
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void run() {
|
||||||
|
// No processing in this block
|
||||||
|
}
|
||||||
|
}
|
@ -0,0 +1,25 @@
|
|||||||
|
package com.baeldung.concurrent.threadlifecycle;
|
||||||
|
|
||||||
|
public class TimedWaitingState {
|
||||||
|
public static void main(String[] args) throws InterruptedException {
|
||||||
|
DemoThread obj1 = new DemoThread();
|
||||||
|
Thread t1 = new Thread(obj1);
|
||||||
|
t1.start();
|
||||||
|
// The following sleep will give enough time for ThreadScheduler
|
||||||
|
// to start processing of thread t1
|
||||||
|
Thread.sleep(1000);
|
||||||
|
System.out.println(t1.getState());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
class DemoThread implements Runnable {
|
||||||
|
@Override
|
||||||
|
public void run() {
|
||||||
|
try {
|
||||||
|
Thread.sleep(5000);
|
||||||
|
} catch (InterruptedException e) {
|
||||||
|
Thread.currentThread().interrupt();
|
||||||
|
e.printStackTrace();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
@ -0,0 +1,35 @@
|
|||||||
|
package com.baeldung.concurrent.threadlifecycle;
|
||||||
|
|
||||||
|
public class WaitingState implements Runnable {
|
||||||
|
public static Thread t1;
|
||||||
|
|
||||||
|
public static void main(String[] args) {
|
||||||
|
t1 = new Thread(new WaitingState());
|
||||||
|
t1.start();
|
||||||
|
}
|
||||||
|
|
||||||
|
public void run() {
|
||||||
|
Thread t2 = new Thread(new DemoThreadWS());
|
||||||
|
t2.start();
|
||||||
|
|
||||||
|
try {
|
||||||
|
t2.join();
|
||||||
|
} catch (InterruptedException e) {
|
||||||
|
Thread.currentThread().interrupt();
|
||||||
|
e.printStackTrace();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
class DemoThreadWS implements Runnable {
|
||||||
|
public void run() {
|
||||||
|
try {
|
||||||
|
Thread.sleep(1000);
|
||||||
|
} catch (InterruptedException e) {
|
||||||
|
Thread.currentThread().interrupt();
|
||||||
|
e.printStackTrace();
|
||||||
|
}
|
||||||
|
|
||||||
|
System.out.println(WaitingState.t1.getState());
|
||||||
|
}
|
||||||
|
}
|
Loading…
x
Reference in New Issue
Block a user