Logo 
Search:

Java Forum

Ask Question   UnAnswered
Home » Forum » Java       RSS Feeds

A question on Synchronization

  Asked By: Robert    Date: Sep 18    Category: Java    Views: 494
  

If I want to use a synchronized map you may suggest me to use HashTable or making a HashMap synchronized by the help of java.util.Collections.synchronizedMap...

But the problem is that these solutions synchronize reads when there is no change on the structure.

I want the followings:
1- Lock the structure when the threads wants to change the structure
2- If a thread is reading there shouldn't be any lock for other reads that want to access the structure simultaneously
3- Of course there shouldn't be any read under progress when a change is going to be done.

Is there any sophisticated and fast solution for this problem?

Share: 

 

8 Answers Found

 
Answer #1    Answered By: Omar Walker     Answered On: Sep 18

You may wrap all accesses with a RW lock. see:

java.sun.com/.../ReadWriteLock\
.html

java.sun.com/.../ReentrantRead\
WriteLock.html

 
Answer #2    Answered By: Bonnie Hughes     Answered On: Sep 18

As I know, there is no pre-built structure  satisfying your requirements (at least in JDK). you have to wrap your map  by your own-built class and for locking use java.sun.com/.../ReentrantReadWriteLock.html. With this class you can completely control your read- and write-locking. The class' javadoc has some samples. I found them useful.

 
Answer #3    Answered By: Percy Morgan     Answered On: Sep 18

I'm not really sure, but I think you should use Synchronized block or method for managing this case instead of just using a synchronized collection, you can synchronized the write methods but let the read  methods be non-synchronized, this may result in what you wanted.

 
Answer #4    Answered By: Aaron Kennedy     Answered On: Sep 18

I am sure your solution   will work, But I want to mention that using synchronized block is deprecated and has no more use. For additional information, read  java.util.concurrency package in JDK Javadocs.

 
Answer #5    Answered By: Ana Silva     Answered On: Sep 18

Thanks a lot for your useful mention,it was really the case I needed to know.

 
Answer #6    Answered By: Dustin Dean     Answered On: Sep 18

I think You should Use ConcurrentHashMap , In your case I think ConcurrentHashMap is a good solution  ...

A hash table supporting full concurrency of retrievals and adjustable expected concurrency for updates. This class obeys the same functional specification as Hashtable, and includes versions of methods corresponding to each method of Hashtable. However, even though all operations are thread-safe, retrieval operations do not entail locking, and there is not any support for locking the entire table in a way that prevents all access. This class is fully interoperable with Hashtable in programs that rely on its thread  safety but not on its synchronization  details.

Retrieval operations (including get) generally do not block, so may overlap with update operations (including put and remove). Retrievals reflect the results of the most recently completed update operations holding upon their onset. For aggregate operations such as putAll and clear, concurrent retrievals may reflect insertion or removal of only some entries. Similarly, Iterators and Enumerations return elements reflecting the state of the hash table at some point at or since the creation of the iterator/enumeration. They do not throw ConcurrentModificationException. However, iterators are designed to be used by only one thread at a time.


java.sun.com/.../ConcurrentHashMap.html

 
Answer #7    Answered By: Ruairidh Anderson     Answered On: Sep 18

If you are using JDK 1.5 or higher this might help:
java.sun.com/.../ConcurrentHashMap.html

 
Answer #8    Answered By: Jay Richards     Answered On: Sep 18

In Java 5 or later, you can use ConcurrentHashMap for this purpose. You can also use an implementation from Javolution package which is called FastMap. Both of them implement ConcurrentMap interface that includes some additional atomic methods such as putIfAbsent and replace.

 
Didn't find what you were looking for? Find more on A question on Synchronization Or get search suggestion and latest updates.