Skip to content

Instantly share code, notes, and snippets.

@chathurangat
Last active December 23, 2017 12:45
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save chathurangat/924862168800ba0d3b3bfa4f6e8611a4 to your computer and use it in GitHub Desktop.
Save chathurangat/924862168800ba0d3b3bfa4f6e8611a4 to your computer and use it in GitHub Desktop.
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