Inter Thread Communication via shared object
Lifetime of thread is only run method .Thread is alive ,after completing run method thread is dead.
When thread is enter into the run method then state of the thread can be:
waiting, waitingtime, blocked and runnable and inside runnable 2 states ready and running. but when we create thread and we do not call the start method or means we don’t call start method then at that time thread is in new state.
Suppose t1 thread has to copy 10 files from the network and t2 thread job is to read those files and counts the word from that file. If we do this work sequentially means thread t1 first copy all the files and after that thread t2 count the words from each file then it will take more time to do this job.so if we want to do this parallely means, t1 thread copy 1 file then thread t2 count the words and so on so how it’s possible ?
It’s possible through sharing common object to communicate between the thread like thread t1 pass signal to the thread t2 that I copy 1 file and now you can read. This is the concept.
DemoThreads.java
package DemoThreads;
class Show {
public synchronized void goodMorning(String name,MySignal mySignal) {
System.out.println("Current thread is "+Thread.currentThread().getName());
System.out.println(Thread.currentThread().getName()+"is waiting for signal");
while(!mySignal.getHasDataToProcess()) {
}
System.out.println(Thread.currentThread().getName()+"is out from signal");
mySignal.setHasDataToProcess(true);
}
}
Show.java
package DemoThreads;
class Show {
public synchronized void goodMorning(String name,MySignal mySignal) {
System.out.println("Current thread is "+Thread.currentThread().getName());
System.out.println(Thread.currentThread().getName()+"is waiting for signal");
while(!mySignal.getHasDataToProcess()) {
}
System.out.println(Thread.currentThread().getName()+"is out from signal");
mySignal.setHasDataToProcess(true);
}
}
MyThread.java
class MyThread extends Thread {
Show s;
String name;
MySignal mySignal;
MyThread(Show s,String name,MySignal mySignal) {
this.s = s;
this.name = name;
this.mySignal = mySignal;
}
public void run() {
s.goodMorning(name,mySignal);
}
}
MySignal.java
class MySignal {
private boolean flag = false;
public synchronized boolean getHasDataToProcess() {
return flag;
}
public synchronized void setHasDataToProcess(boolean flag) {
this.flag = flag;
}
}
Threads.java
public class Threads {
public static void main(String args[]) throws InterruptedException {
Show s = new Show();
MySignal signal = new MySignal();
MyThread t1 = new MyThread(s,"ankit",signal);
MyThread t2 = new MyThread(s,"agam",signal);
t1.start();
t2.start();
Thread.sleep(2000);
System.out.println("Giving Signal");
signal.setHasDataToProcess(true);
Thread.sleep(2000);
System.out.println("Giving Signal");
signal.setHasDataToProcess(true);
}
}
Output
Current thread is Thread-0
Thread-0is waiting for signal
Giving Signal
Thread-0is out from signal
Current thread is Thread-1
Thread-1is waiting for signal
Thread-1is out from signal
Giving Signal