Results 1 to 6 of 6
  1. #1
    JavaVox is offline Member
    Join Date
    Jun 2011
    Posts
    5
    Rep Power
    0

    Question EOFException error in I/O. Are threads the problem?

    Hi,

    I have been using the following code for years now with now problem. Suddenly now that I am trying
    to use it with 2 threads I get an EOFException. I thought that threads were supposed to be mutually
    exclusive (in other words one cannot run until another one has finished). Both of my threads use
    this code. Can anyone suggest a way that I could make sure only one thread at a time is trying
    to I/O? I am new to threads.

    Java Code:
    public static void writeObj(Vector v, String s){
    
            String dataloc2 = s;
    
            Vector ObjectVector2 = new Vector();       
            ObjectVector2 = v;      
         
            try
            {
                FileOutputStream fos5 = new FileOutputStream(dataloc2);
                ObjectOutputStream oos5 = new ObjectOutputStream(fos5);
                oos5.writeObject(ObjectVector2);
                oos5.flush();
                oos5.close();
                System.out.println("wrote "+dataloc2);
     
            }
            catch(Exception exception)
            {
                System.out.println("Exception during serialization: " + exception);
                System.exit(0);
            }
    
     } // end of method
    Thanks in advance for your help!

  2. #2
    kjkrum's Avatar
    kjkrum is offline Senior Member
    Join Date
    Apr 2011
    Location
    Tucson, AZ
    Posts
    1,060
    Rep Power
    6

    Default

    If you made that method synchronized, only one thread at a time could call it or any other synchronized static method of the same class. Note that this would not prevent another thread from opening the same file by some other means.
    Get in the habit of using standard Java naming conventions!

  3. #3
    JavaVox is offline Member
    Join Date
    Jun 2011
    Posts
    5
    Rep Power
    0

    Default

    Ok thanks for the advice. I put replaced public with synchronized and it still compiled, so will try to run it.

    PS: not sure what you mean by "Get in the habit of using standard Java naming conventions! Except #7 and maybe #8. Those are kind of dumb."

    What are number 7 and number 8?

    Thanks again for the advice.

  4. #4
    DarrylBurke's Avatar
    DarrylBurke is offline Member
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,184
    Rep Power
    19

    Default

    That's kjkrum's signature line, not part of the response to your question.

    What are number 7 and number 8?
    Click the link if you really want to know. I prefer Code Conventions for the Java(TM) Programming Language: Contents myself.

    db

  5. #5
    JavaVox is offline Member
    Join Date
    Jun 2011
    Posts
    5
    Rep Power
    0

    Default

    Yeah sorry, I figured that out after I sent the post.....

  6. #6
    kjkrum's Avatar
    kjkrum is offline Senior Member
    Join Date
    Apr 2011
    Location
    Tucson, AZ
    Posts
    1,060
    Rep Power
    6

    Default

    You don't need to replace public with synchronized. A method can be both.
    Get in the habit of using standard Java naming conventions!

Similar Threads

  1. java.io.EOFException using readObject
    By yotamoo in forum Advanced Java
    Replies: 0
    Last Post: 12-31-2010, 10:00 AM
  2. Object Input Stream EOFException
    By FlyNn in forum New To Java
    Replies: 1
    Last Post: 12-18-2010, 12:33 PM
  3. Timers, Networking, Threads Error
    By Dan0100 in forum New To Java
    Replies: 4
    Last Post: 09-22-2010, 11:28 PM
  4. Replies: 0
    Last Post: 04-05-2009, 03:59 PM
  5. EOFException
    By coyne20 in forum Introductions
    Replies: 0
    Last Post: 02-19-2009, 02:02 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •