Win a copy of Five Lines of Code this week in the OO, Patterns, UML and Refactoring forum!
  • 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 all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Bear Bibeault
  • Ron McLeod
  • Jeanne Boyarsky
  • Paul Clapham
Sheriffs:
  • Tim Cooke
  • Liutauras Vilda
  • Junilu Lacar
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • fred rosenberger
  • salvin francis
Bartenders:
  • Piet Souris
  • Frits Walraven
  • Carey Brown

java.io.StreamCorruptedException: invalid stream header:i understand its a thread issue not sure fix

 
Ranch Hand
Posts: 55
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have a SerializeObject class that reads and writes objects to a file. I have a problem where if i have two threads running both trying to read from the file i get that exception. I understand the basic conflict not sure how to fix. do i run a wait on the ObjectInputStream then once i know its closed let next call to readObject read from file.

My threads knowledge is subpar i want to learn more on how to implement these thread like issues.

teach me o wise ones!

 
steve labar
Ranch Hand
Posts: 55
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
so i was just thinking about how im calling these.

is it better to create a separate thread for reading the file and run wait on that or have the outputstream itself handle waiting inside the method?
 
Ranch Hand
Posts: 261
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Well, if you want to have a thread safe implementation for your scenario, here's one I can suggest -

Have a SerialiserThread class. Something like this -

The AtomicBoolean takes care that the boolean value gets updated atomically in a multi-threaded environment.
Have a driver class which handles all the incoming threads requesting for SerializeObjects.


Currently, this driver class will make other threads wait indefinitely for another thread which is busy with the SerializeObject class, so you might want to put in a logic for having a timed wait using join(), or sleep() methods.

There can be some other ways to achieve this as well. Some might even be better. But for now, you can probably give this method a try.
 
The world's cheapest jedi mind trick: "Aw c'mon, why not read this tiny ad?"
Building a Better World in your Backyard by Paul Wheaton and Shawn Klassen-Koop
https://coderanch.com/wiki/718759/books/Building-World-Backyard-Paul-Wheaton
    Bookmark Topic Watch Topic
  • New Topic