2

In my java module, multiple threads are calling string.intern(), where each thread has their own copy of string object. After calling string.intern() these threads attempt to take a lock on that. However I have not found any documentation that discusses about thread safety of this intern(). My worry is, will I end up pushing duplicate string in thread pool?

EDIT : Let me explain the scenario.

The threads that I referred above are workitem threads generated by some job. These thread contain unique string identifier. Threads generated from a particular job will have same unique identifier. When these threads are spawned by job, they all point to same unique identifier. I am using this unique identifier as a lock to synchronize these threads. However, these workitem thread go through cycle of serialization and de-serialization. What I found that a unique identifier (which is string) of a de-serialized thread now no longer refers to a copy from thread pool. It has its own copy of unique identifier. Now my attempt to take lock on unique identifier is futile because I have more than one string objects of unique identifier.

string.intern() is an attempt to push this string object in thread pool and then use it as lock.

Mayur
  • 75
  • 1
  • 10
  • May I ask what made you decide on using String intern() since it is very rarely used. afaik, it has minimal performance gains when multiple String objects have the same value, interning them will cause all references to point to the same object – Sharon Ben Asher Jun 10 '15 at 06:13
  • 1
    Locking on interned strings doesn't sound like a good idea... – assylias Jun 10 '15 at 06:21
  • @sharonbn " interning them will cause all references to point to the same object" That is exactly the idea. Have all string references point to same object and then take a lock on that. I have elaborated this in my question. – Mayur Jun 10 '15 at 06:35

0 Answers0