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

  • Home
  • SEARCH
  • 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 56417
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 10, 20262026-05-10T17:31:52+00:00 2026-05-10T17:31:52+00:00

Basically, I have a class with 2 methods: one to serialize an object into

  • 0

Basically, I have a class with 2 methods: one to serialize an object into an XML file and another to read an object from XML. Here is an example of synchronized part from the method that restores an object:

    public T restore(String from) throws Exception {      // variables declaration         synchronized (from) {             try {                 decoder = new XMLDecoder(new BufferedInputStream(                         new FileInputStream(from)));                 restoredItem = decoder.readObject();                 decoder.close();             } catch (Exception e) {                 logger.warning('file not found or smth: ' + from);                 throw new Exception(e);             }         }     // try to cast it     } 

A similar approach is taken when serializing an object. Now, when I create a unit test which in turn creates 10 threads with each thread trying to serialize and instantly read either a Boolean or a String it would fail showing that ClassCastExceptions occur. This makes me think that I get serialization wrong (everything’s ok in a single-threaded environment). If you’ve stayed with me down to this point :), here are the 2 issues I need your help on:

  1. does it make sense to synchronize on a string argument passed to method (considering there’s a string pool in java)? BTW, I’ve tried synchronizing on the XMLSerializer class itself with result staying the same.
  2. how do i correctly synchronize io operations on a single file?
  • 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. 2026-05-10T17:31:53+00:00Added an answer on May 10, 2026 at 5:31 pm

    1. Yes, it’s OK to synchronize on a String, however you’d need to synchronize on the string.intern() in order to always get the same Object

    StringBuffer sb = new StringBuffer(); sb.append('a').append('b'); String a = new String(sb.toString()); String b = new String(sb.toString()); a == b; //false a.equals(b); //true a.intern() == b.intern(); //true 

    Since you want to synchronize on the same monitor, you’d want the intern().

    2. You’d probably not want to synchronize on a String since it may synchronized on somewhere else, inside your code, in 3rd party or in the JRE. What I’d do, if I wanted to stay with synchronize, is create an ID class (which may hold only String), override equals() and hashcode() to match, put it in a WeakHashMap with both as key and value (see IdentityHashMap for idea why) and use only what I .get() from the map (sync map{ syncKey = map.get(new ID(from)); if syncKey==null create and put new key} sync{syncKey}).

    3. Then again, I’d ditch synchronize all together and use the java.util.concurrent.locks.Lock instead, in the same setup as above only with the lock attached to the ID.

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

Sidebar

Ask A Question

Stats

  • Questions 92k
  • Answers 92k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer I suppose the question is not really "is the standrard… May 11, 2026 at 6:26 pm
  • Editorial Team
    Editorial Team added an answer You'll need to convert to text - perhaps XML. Then… May 11, 2026 at 6:26 pm
  • Editorial Team
    Editorial Team added an answer There could be couple of options here. Bean method can… May 11, 2026 at 6:26 pm

Related Questions

I am having some trouble with one of my ASP.NET 2.0 application's connection string.
I have an application written in C#.Net (Framework 2.0 if it matters). It calls
I'm using Delphi 2007 Pro. I have a runtime package that includes a number
I have been assigned a project to develop a set of classes that act

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

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.