Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations sizbut on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

thread deadlocks on solaris but not on windows

Status
Not open for further replies.

frag

Programmer
Dec 7, 2000
321
GB
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:

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(&quot;Thread POP\t, Element: &quot; + (String)oh.getElement());
  	 i++;
  	}
  	System.out.println(&quot;Thread POP died!&quot;);
  }

}


public class Thread_PushElement extends Thread {

  VectorHandler oh;
  
  public Thread_PushElement(VectorHandler oh)
  {
  	this.oh = oh;
  }

  public void run()
  {
  	String str = &quot;&quot;;
  	for( int i = 0; i < 100; i++ )
  	{
  	 str = &quot;&quot; + i;
  	 oh.putElement(str);
  	 System.out.println(&quot;Thread PUSH\t, Element: &quot; + str);
  	}
  	System.out.println(&quot;Thread PUSH died!&quot;);
  }

}



public class Thread_ReadElement extends Thread {

  VectorHandler oh;
  
  public Thread_ReadElement(VectorHandler oh)
  {
  	this.oh = oh;
  }

  public void run()
  {
  	String str = &quot;&quot;;
  	int i = 0;
  	while( oh.readElement(i) != null )
  	{
  	 str = (String)oh.readElement(i);
  	 System.out.println(&quot;Thread READ\t, Element: &quot; + str);
  	 i++;
  	}
  	System.out.println(&quot;Thread READ died!&quot;);
  }

}



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(&quot;Main thread died!&quot;); 
  }


}


cheers

frag

patrick.metz@epost.de
 
Hi frag,

I've run your code fine on the below :

Win2000 with SDK 1.4.1_02
Win2000 with SDK 1.3.1_07
Linux RedHat 8 with SDK 1.4.1_02
Solaris 9 (OS 5.9) with SDK 1.4.0

And it all seems to run with no problems ... sorry, not much help ...
 
Hi sedj,

thank you for your efforts. This is realy strange.
Our Solaris machine has OS version 5.8 (don't know if this is Solaris 8 or 9) and the java version is the build 1.3.1_02-b02.

It starts with pushing 28 elements in the vector and after that one element gets popped from the vector... and that's all, deadlock.

Could this be a bug in the java build or is it possible that the machine has just not enough performance? A lot of people are working on this machine and there is always a high load on the CPU and only a little memory available. This thing is swapping memory to harddisk the hole time.

cheers

frag

patrick.metz@epost.de
 
Well, I would tend to lean to thinking that this is a machine/config problem rather than the JVM/SDK, considering the OS/SDK versions now tested.

Can you add in debug so that it monitors memory usuage -

Runtime.getRuntime().totalMemory();

or maybe start the JVM with more initial memory -
java -Xmx256m ...
 
The problem is solved!

I asked our system administrator to install the newest java release (build 1.4.2_02-b03) and now it runs fine.

Really strange! :-/

patrick.metz@epost.de
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top