Sumit Singh
Sumit Singh

Reputation: 15886

ArrayList inside HashTable is thread safe

Simple question. Is following code is thread safe...?

  1. If yes, is there is any better way..?
  2. If no, why.? and how can I make it thread safe.

My main doubt because of ArrayList inside Hashtable because ArrayList is not thread safe. So same thing will happen if its part of Hashtable.

Hashtable<Thread, List<String>> threadObjects = new Hashtable<Thread, List<String>>();
 // lets assume some object is added. 
 synchronized (threadObjects)
  {
     thread = Thread.currentThread();
     List<String> v =  threadObjects.get(thread);
     if (null != v)
     {
       // do something
     }
  }

Thanks

Upvotes: 0

Views: 315

Answers (3)

cambecc
cambecc

Reputation: 4184

You can use a ThreadLocal to store thread-specific data. For example:

private final ThreadLocal<List<String>> threadObjects =
    new ThreadLocal<List<String>>() {
        @Override protected List<String> initialValue() {
            return new ArrayList<String>();
        }
    };

public void foo() {
    for (String s : threadObjects.get()) {
        // do something with each string in this thread's list
    }
}

This is a much better way of tracking thread-specific data than the Hashtable approach because the table is maintained for you inside the ThreadLocal implementation itself, including the synchronization. No additional synchronization is needed on the ArrayList instance as long as you don't publish the reference to other threads; each thread will have its own ArrayList instance to do with as it pleases without concurrency concerns.

The overriding of initialValue() is a convenience because it allows you to avoid the check for null on the result of threadObjects.get(), assuming of course that you don't explicitly set the thread local to null.

Upvotes: 3

Stephen C
Stephen C

Reputation: 718758

Is following code is thread safe...?

Yes ... provided that:

  • the code that creates the map and adds entries to the map is also thread-safe,
  • no other code uses the map without synchronizing on it (as per your code), and
  • no code updates the list objects without synchronizing on the map.

Thread-safety of an externally synchronized data structure can only be determined by examining all of the code that uses the data structure.

As Peter states, ThreadLocals would be a better solution, but there is still a potential thread-safety issue if the list objects are mutated without appropriate locking.

Upvotes: 4

Peter Lawrey
Peter Lawrey

Reputation: 533492

If yes, is there is any better way..?

Yes, but use a ThreadLocal>

My main doubt because of ArrayList inside Hashtable because ArrayList is not thread safe.

Normally you would be right, however this code ensures that an ArrayList is only accessed by one thread. Of course if you didn't following this pattern it would be thread safe.

Note: The synchronized (threadObjects) doesn't make any difference in this case.

Upvotes: 5

Related Questions