Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6119577
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T15:34:10+00:00 2026-05-23T15:34:10+00:00

All, I am writing a multithread program. The class which extends Thread has a

  • 0

All,

I am writing a multithread program. The class which extends Thread has a static Hashtalbe<Integer, SessionData> sessionDataTable.

In the class, I will do something to change the sessionDataTable, like insert a new SessionData object into it, delete a SessionData object from it or modified the SessionData objects in the Hashtable.

At the end, I will write the Hashtable to a file using ObjectOutputStream, the method is something like

public static synchronized void saveDataSessionState()
{
    ...
    ObjectOutputStream oos = new ObjectOutputStream(new FileOutputStream(Constants.logFile));
    oos.writeObject(sessionDataTable);
    ...
}

I am wondering what will happen if one thread is doing oos.writeObject(sessionDataTable); but other threads are modifying the sessionDataTable (like what I said above) at the same time somewhere outside this method. Will the above method cause exception when writing the object to file?

If yes, how could I avoid the problem? Use lock? But then I need to put a lock whenever I modify the Hashtable.

Thanks.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-23T15:34:11+00:00Added an answer on May 23, 2026 at 3:34 pm

    Based on my examination of the code, I think that you won’t have any problems. The accessors and mutators for the Hashtable class are declared as synchronized, and so is the private writeObject method on Hashtable that does the serialization. That should be sufficient to ensure that nothing can change the Hashtable while the serializer is looking at it.

    However, you could get into trouble if something changed the state of a Hashtable key or value while serialization was occurring. (I don’t know if default serialization of an object is performed holding an object’s mutex. I suspect not because of the cost of locking unnecessarily, the fact that a simple mutex won’t necessarily achieve the required exclusive access, and the potential risk of deadlocks.)

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm writing some code for a class constructor which loops through all the properties
I'm writing a program which creates no forms at all until one is required.
I am writing this java program to find all the prime numbers up to
I'm currently writing a class to handle all database-activity in my application, and so
Im using Androids built in way of handling preferences which works by writing all
I am writing a software which stores all the information of a users interaction
I am working on a solo project, writing all code myself so no collaboration
I am writing an application where all the request to the internet should go
I hear people writing these programs all the time and I know what they
Should I be writing Doc Comments for all of my java methods?

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.