Writing an object to a file when other threads are modifying the object
<p>All,</p>

<p>I am writing a multithread program. The class which extends Thread has a <code>static Hashtalbe&lt;Integer, SessionData&gt; sessionDataTable</code>.</p>

<p>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.</p>

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

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

<p>I am wondering what will happen if one thread is doing <code>oos.writeObject(sessionDataTable);</code> 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?</p>

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

<p>Thanks.</p>
