Skip to content

Instantly share code, notes, and snippets.

@chathurangat
Last active December 20, 2017 19:27
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/fe8c34dba9c0ff828e2ec96f289d839b to your computer and use it in GitHub Desktop.
Save chathurangat/fe8c34dba9c0ff828e2ec96f289d839b to your computer and use it in GitHub Desktop.
public class Application
{
public static void main(String[] args)
{
System.out.println("Main thread started ");
ThreadA threadA = new ThreadA();
threadA.start();
System.out.println("ThreadA was started");
synchronized (threadA)
{
try {
System.out.println("Main thread is waiting for ThreadA ");
threadA.wait();
System.out.println("Main thread resumed again");
} catch (InterruptedException e) {
System.out.println("waiting thread interrupted ");
}
}
System.out.println("Main thread completed ");
}
}
class ThreadA extends Thread
{
public void run()
{
synchronized (this) {
System.out.println("ThreadA is running");
notify();
System.out.println("ThreadA called notify() on its own");
try {
System.out.println("ThreadA sleeps for 5 seconds");
Thread.sleep(5000);
} catch (InterruptedException e) {
System.out.println("sleeping thread interrupted ");
}
System.out.println("ThreadA synchronization block completed");
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment