Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
package com.baeldung.concurrent.threadlifecycle;

public class BlockedState {
public static void main(String[] args) throws InterruptedException {
Thread thread1 = new Thread(new DemoThreadB());
Thread thread2 = new Thread(new DemoThreadB());

thread1.start();
thread2.start();

Thread.sleep(1000);

System.out.println(thread2.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 'thread1' won't leave this method
// when Thread 'thread2' enters this
}
}
}
Original file line number Diff line number Diff line change
@@ -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() {

}
}
Original file line number Diff line number Diff line change
@@ -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() {

}
}
Original file line number Diff line number Diff line change
@@ -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
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
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) {
e.printStackTrace();
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
package com.baeldung.concurrent.threadlifecycle;

public class WaitingState implements Runnable {
public static Thread thread1;

public static void main(String[] args) {
thread1 = new Thread(new WaitingState());
thread1.start();
}

public void run() {
Thread thread2 = new Thread(new DemoThreadWS());
thread2.start();

try {
thread2.join();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}

class DemoThreadWS implements Runnable {
public void run() {
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}

System.out.println(WaitingState.thread1.getState());
}
}