Last active
December 23, 2017 12:45
-
-
Save chathurangat/924862168800ba0d3b3bfa4f6e8611a4 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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