• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Blocking an InputStream

 
Ranch Hand
Posts: 296
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Ranch Hand
Posts: 1365
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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:
 
Wanderer
Posts: 18671
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Drew Lane
Ranch Hand
Posts: 296
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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.
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic