Inter Thread Communication in Java

Inter-thread Communication can be defined as the exchange of message between two or more threads. The transfer of messages takes place before or after the change of state of a thread. Java implements Inter-thread Communication with the help of the following three methods:
[dropshadowbox align=”none” effect=”raised” width=”auto” height=”” background_color=”#ffffff” border_width=”1″ border_color=”#dddddd” ]1. notify(): It resumes the first thread that went into the sleep mode. It has the following syntax:

final void notify()

2. notify(): It resumes all the threads that are in sleep mode. The execution of these threads happens as per priority. It has the following syntax:

final void notifyall()

3. wait(): It sends the calling thread into the sleep mode. This thread can now be activated only by notify() or notifyall() methods. It has the following syntax:

final void wait()

[/dropshadowbox]

Implementing the Runnable Interface:

class A implements Runnable 
{
 public void run()
{
 for(int i=1;i<=10;i++)
{
 System.out.println("ThreadA"+i);
}
System.out.println("End of ThreadA");
}
}
class Test
{
 public static void main(String args[])
{
 A runnable = new A;
 Thread threadA=new Thread (runnable);
 threadA.start();
 System.out.println("End of main thread");
}
}