Problem with threads
Hi,
I am facing some issues with the multithreading.
I have 3 class extending threads (say class1,2,3)
Class 1 starts its thread and inside the run() it starts the other two thread.
Two vectors are shared between these class
Verctor1 - Thread1 (class1) and Thread2 (class2)
Verctor2 - Thread1 (class1) and Thread3 (class2)
public class1 {
public void run() {
class2 c2;
class3 c3;
while(true) {
//if no line to read then continue
//Read a line from a file
//Add the line to vector
if(c2.isSleeping() == 1)
c2.startProcessing();
if(c3.isSleeping() == 1)
c3.startProcessing();
}
}
}
public class2 {
public void run() {
//Some work
if(vector1.size() == 0)
holdProcessing();
//Some work
}
public synchronized void startProcessing(){
notify();
slept = false;
}
private synchronized void holdProcessing(){
try {
slept = true;
wait();
} catch (InterruptedException e){
e.printStackTrace();
}
}
public synchronized boolean isSleeping(){
return slept;
}
}
public class3 {
public void run() {
//Some work
if(vector2.size() == 0)
holdProcessing();
//Some work
}
public synchronized void startProcessing(){
notify();
slept = false;
}
private synchronized void holdProcessing(){
try {
slept = true;
wait();
} catch (InterruptedException e){
e.printStackTrace();
}
}
public synchronized boolean isSleeping(){
return slept;
}
}
The thread1 will get data from a file, which is inturn periodically updated by another process.
Thread 1 will sleep for 10 sec if there is no data to read. If thread1 got any data to read it will read a line and add it to vector 1 and 2 and notify thread 2 and 3.
But in few cases even after thread 2 and 3 got notified its in state of sleep and not taking any data from the vector.
Can any one help me to fix this?

