Hi!
I wrote a little consumer-producer example just to learn how to handle this kind of problem. The application runs perfect on a Windows2000 machine with Java 1.4.2 but if the same application is executed in a solaris environment with Java 1.3.1 the application hangs up (deadlock?).
Why is it like that?
Here are my classes:
cheers
frag
patrick.metz@epost.de
I wrote a little consumer-producer example just to learn how to handle this kind of problem. The application runs perfect on a Windows2000 machine with Java 1.4.2 but if the same application is executed in a solaris environment with Java 1.3.1 the application hangs up (deadlock?).
Why is it like that?
Here are my classes:
Code:
import java.util.*;
public class VectorHandler{
private Vector myVec = new Vector(100,10);
public synchronized Object getElement()
{
Object obj;
while( myVec.isEmpty() )
{
try{
wait();
}catch( InterruptedException e){
}
}
obj = myVec.elementAt(0);
myVec.removeElementAt(0);
return obj;
}
public synchronized Object readElement(int i)
{
Object obj;
if( i < myVec.size() )
{
return myVec.elementAt(i);
}
else
{
return null;
}
}
public synchronized void putElement(Object obj)
{
myVec.add(obj);
notify();
}
}
public class Thread_PopElement extends Thread {
VectorHandler oh;
public Thread_PopElement(VectorHandler oh)
{
this.oh = oh;
}
public void run()
{
int i = 0;
while( i < 100 )
{
System.out.println("Thread POP\t, Element: " + (String)oh.getElement());
i++;
}
System.out.println("Thread POP died!");
}
}
public class Thread_PushElement extends Thread {
VectorHandler oh;
public Thread_PushElement(VectorHandler oh)
{
this.oh = oh;
}
public void run()
{
String str = "";
for( int i = 0; i < 100; i++ )
{
str = "" + i;
oh.putElement(str);
System.out.println("Thread PUSH\t, Element: " + str);
}
System.out.println("Thread PUSH died!");
}
}
public class Thread_ReadElement extends Thread {
VectorHandler oh;
public Thread_ReadElement(VectorHandler oh)
{
this.oh = oh;
}
public void run()
{
String str = "";
int i = 0;
while( oh.readElement(i) != null )
{
str = (String)oh.readElement(i);
System.out.println("Thread READ\t, Element: " + str);
i++;
}
System.out.println("Thread READ died!");
}
}
public class SyncTest{
public static void main(String [] arstring)
{
VectorHandler oh = new VectorHandler();
Thread_PopElement pop = new Thread_PopElement(oh);
Thread_PushElement push = new Thread_PushElement(oh);
Thread_ReadElement read = new Thread_ReadElement(oh);
Thread_PushElement push_again = new Thread_PushElement(oh);
push.start();
pop.start();
while( push.isAlive() || pop.isAlive() ){
}// do nothing... just wait
push_again.start();
while( push_again.isAlive() ){
}// do nothing... just wait
read.start();
while( read.isAlive() ){
}// do nothing... just wait
System.out.println("Main thread died!");
}
}
cheers
frag
patrick.metz@epost.de