thread communication java

Thread communication java:



In java using three method wait(),notify(),notifyAll() therads can comnicate
these methods avilaible in object class.


what is the benefit of thread communication?


performance is the key  benefit of thread communication
example one thread is reading content in a file and creating object
and another thread saving those details into a database .
after creating first object only second thread need to continue if second thread every time is checking for first thread  means whether object creation done
by first thread or not? its time taking process
using this wait thread no need to check every time after first thread finish his work its invoke notify
after that second thread continues his work.
   when second thread call wait method thread its self its going to wait room until other thread(first)thread notifies.
A thread cant invoke  wait and  notify methods on an object unless it owns the object lock.
this mean these method should call from synchronized area.


How this communication happens?

Based on the object lock only.when ever we call wait method on current object
it release the lock immediately and enters into waiting state.
when ever we call notify on current thread it releases the lock but not immediately if any other
code is left in synchronized are for that particular method it completes execution of
synchronized code after that it release the lock

we can call wait method by passing time milli second after that particular time again thread enter into
runnable sate even if notify method not called also.

Example
:
package com.multi;
public class ThreadComnication extends Thread{
static int  total=0;
public static void main(String[] args) {
ThreadComnication comnication=new ThreadComnication();
comnication.setName("suri");
comnication.start();
synchronized(comnication){
try {
comnication.wait(1000);//thread release the lock and waits only one second after that again it moves to runnable state.
} catch (InterruptedException e) {
// TODO Auto-generated catch block
System.out.println("");
e.printStackTrace();
}
}
System.out.println("Total---amount printing by--"+Thread.currentThread().getName()+"Thread-->"+total);
}
@Override
public void run(){
for(int i=1;i<=10;i++){
System.out.println("Total amount calculating  by "+Thread.currentThread().getName());

total=total+10;
synchronized (this) {
notify();
}
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}


Output:


Total amount caluculating  by suri
Total---amount printing by--mainThread-->10
Total amount calculating  by suri

Total amount calculating  by suri

Total amount calculating  by suri

Total amount calculating  by suri

Total amount calculating  by suri

Total amount calculating  by suri

Total amount calculating  by suri

Total amount calculating  by suri

Total amount calculating  by suri





Author

Written by Admin

Aliquam molestie ligula vitae nunc lobortis dictum varius tellus porttitor. Suspendisse vehicula diam a ligula malesuada a pellentesque turpis facilisis. Vestibulum a urna elit. Nulla bibendum dolor suscipit tortor euismod eu laoreet odio facilisis.

0 comments: