Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
public class Application
{
public static void main(String[] args)
{
System.out.println("main thread is started");
ThreadA threadA = new ThreadA();
threadA.start();
ThreadB threadB = new ThreadB(threadA);
threadB.start();
System.out.println("main thread completed");
}
}
class ThreadA extends Thread
{
public void run()
{
System.out.println("ThreadA is running");
try {
System.out.println("ThreadA is sleeping for 15 seconds ");
Thread.sleep(15000);
System.out.println("ThreadA is waked up ");
} catch (InterruptedException e) {
System.out.println("Sleep of ThreadA get interrupted ");
}
System.out.println("ThreadA completed");
}
}
class ThreadB extends Thread
{
private ThreadA threadA;
ThreadB(ThreadA threadA)
{
this.threadA = threadA;
}
public void run()
{
System.out.println("ThreadB is running");
try {
System.out.println("ThreadB is sleeping for 5 seconds");
Thread.sleep(5000);
System.out.println("ThreadB wakes up from sleep and interrupts ThreadA ");
threadA.interrupt();
} catch (InterruptedException e) {
System.out.println("Sleep of ThreadB get interrupted");
}
System.out.println("ThreadB completed");
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment