permaculture playing cards*
The moose likes Threads and Synchronization and the fly likes Blocking an InputStream Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Threads and Synchronization
Bookmark "Blocking an InputStream" Watch "Blocking an InputStream" New topic
Author

Blocking an InputStream

Drew Lane
Ranch Hand

Joined: May 13, 2001
Posts: 296
I'm not real good with multithreading, so please bare with me.
I'm using the following code to read a file that's in a thread that's continuously running:
for (int i = 0, n = fsize; i<n; i++) {
buf[i] = (byte)dis.read();
}
I want to block all other threads until the for loop is completed.
How do I specify this?
If i put a synchronized{} block around it, will that do the trick?
Thanks,
Drew
David Weitzman
Ranch Hand

Joined: Jul 27, 2001
Posts: 1365
Doug Lea's util.concurrent has a Barrier interface for this sort of thing.
Here's a quick example of doing it by hand. Example.class is used as a barrier. I originally used an anonymous object as a barrier and declared notDone volatile but I wasn't sure if you're familiar with what volatile does:
Jim Yingst
Wanderer
Sheriff

Joined: Jan 30, 2000
Posts: 18671
Other possibilities include:
Don't start the other threads until the read() loop is done. Which means you don't even really need to create a separate thread for the read loop - just put it in whatever thread you're creating the other threads from. This makes sense if there isn't anything that the other threads can really do until after the read is done.
Alternately, if there is a good reason for the other threads to already be started and then wait, you can use the Thread method join(). This requires that each of the waiting threads have a reference to the Thread they're waiting for. (Not just a Runnable or some other monitor object.)

The barrier implementation David showed is probably more flexible, since most of the time you're already going to have a reference to whatever object you want to use as a monitor for synchronization - but you don't necessarily have a reference to the master thread (unless you pass it in beforehand). But if you do have a reference to the master, join() is pretty simple to use.


"I'm not back." - Bill Harding, Twister
Drew Lane
Ranch Hand

Joined: May 13, 2001
Posts: 296
Hmmm...this seems more complicated than I had hoped. I probably didn't explain this very well, either.
I actually don't have any other of my own threads running at this point (I was actually referring to the AWT thread, etc.).
So, the main thing I want to accomplish is that the for loop is completed and the buf array is always full when the user tries to save the file by pressing a button. What's been happening is that sometimes the array gets partially full and I get a partial (corrupt) file saved.
Unfortunately, I have to use that for loop to read() the file due to some bugs in the implementation I'm using. It's the only method that seem to work consistantly.
From looking at the code that was posted, I like the idea of the wait/notify. Can I use this concept with the gui I made so that when the user pushes the button to save the file, it will wait until it's notified by the thread that the for loop has completed?
Maybe I just need to make the run method synchronized?
I hope this is more understandable.
Regards,
Drew
Andrew Trumper
Greenhorn

Joined: Sep 08, 2001
Posts: 12
Originally posted by Drew Lane:
Hmmm...this seems more complicated than I had hoped. I probably didn't explain this very well, either.
I actually don't have any other of my own threads running at this point (I was actually referring to the AWT thread, etc.).
So, the main thing I want to accomplish is that the for loop is completed and the buf array is always full when the user tries to save the file by pressing a button. What's been happening is that sometimes the array gets partially full and I get a partial (corrupt) file saved.
Unfortunately, I have to use that for loop to read() the file due to some bugs in the implementation I'm using. It's the only method that seem to work consistantly.
From looking at the code that was posted, I like the idea of the wait/notify. Can I use this concept with the gui I made so that when the user pushes the button to save the file, it will wait until it's notified by the thread that the for loop has completed?
Maybe I just need to make the run method synchronized?
I hope this is more understandable.
Regards,
Drew




This should do what you're looking for although the implementation is cheese.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Blocking an InputStream