Bug in synchronized() ?

Tim Bitson <[email protected]>
Newsgroups gmane.comp.hardware.microcontrollers.tini
Message-ID <[email protected]>
I've built a multithreaded HTTP server. I've noticed that I get a 
several unexplained java.io.IOExceptions in my log file. I've traced 
the problem to a synchronized block. It seems that if second thread 
tries to access the synchronized block, the first thread continues to 
run for a while, then freezes for 90 seconds. The first thread then 
throws a IOException, and then the second thread continues. This 
problem is repeatable and consistant. I've run this same routine on 
another Java platform, and it does not exhibit this phenomena.

Here's the code snippet. The lock object is passed from another method 
that writes the file I'm trying to send. In this example, the lock 
object is not locked.
.
.
.
  // read the file and send it out the socket
System.out.println("Want to Open " + webPage);

synchronized(lock)
  {
	// open the file
	System.out.println("Opening " + webPage);
	InputStream fileInputStream = new FileInputStream(webPage);

	// get the number of bytes
	int n = fileInputStream.available();
	.
	.
	.
	while ((n = fileInputStream.read(buffer, 0, 1024)) > -1)
	{
		System.out.println("Read " + n + " Bytes");
		socketOutputStream.write(buffer, 0, n);
		bytesSent += n;
		System.out.println("Sent " + bytesSent + " Bytes");
	}
	System.out.println("Done in " + (System.currentTimeMillis() - start));
	.
	.
	.
}



Here's the the debug printout:

New Request, Opening Sockets		<--- first thread starts
Want to Open /web/dateLineApp.jar		<--- first thread wants to open file
Opening /web/dateLineApp.jar			<--- and it gets it
Read 1024 Bytes
Sent 1024 Bytes
Read 1024 Bytes
Sent 2048 Bytes
.
.
.
Read 1024 Bytes
Sent 54272 Bytes
Read 1024 Bytes
Sent 55296 Bytes
Read 1024 Bytes
New Request, Opening Sockets		<--- second thread starts
Want to Open /web/dateLineApp.jar		<--- second thread wants file, is 
blocked
Sent 56320 Bytes					<--- first thread continues for a while
Read 1024 Bytes
Sent 57344 Bytes
.
.
.
Read 1024 Bytes
Sent 74752 Bytes
Read 1024 Bytes					<--- first thread hangs here for 90 seconds
java.io.IOException  					<--- after 90 seconds, first thread throws 
exception
Opening /web/dateLineApp.jar			<--- second thread is now unblocked & 
runs
Read 1024 Bytes
Sent 1024 Bytes
Read 1024 Bytes
Sent 2048 Bytes
.
.
.
Read 1024 Bytes
Sent 75776 Bytes
Read 143 Bytes
Sent 75919 Bytes
Done in 5960					<--- second thread completes OK
Completed Get Request

Any ideas???

Tim Bitson
Tucson, AZ

_______________________________________________
TINI mailing list
TINI-6tN4nzCoH/[email protected]
To UNSUBSCRIBE, edit your profile, or see list archives:
http://lists.dalsemi.com/mailman/listinfo/tini
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.