I am not sure I understand Synchronized Lists in Java. Suppose I have the following:
List<Integer> numbers = Collections.synchronizedList(new ArrayList<Integer>());
// Assumption: This is running on a separate thread
public void add() {
numbers.add(new Random().nextInt(100));
}
// This is also running on a separate thread
public void doSomething() {
synchronized(numbers) {
for (int i : numbers) {}
}
}
Basically, will the add() be able to add numbers to the list if doSomething() is invoked? What would happen if I instead used public synchronized void add() and public synchronized void doSomething()?
I am working on a UDP Socket Server, and I was going to store clients in an ArrayList. I would have multiple threads that can read, write, and modify this list. What should I be doing?
will the add() be able to add numbers to the list if doSomething() is invoked?
No, not until the thread invoking doSomething() leaves the synchronized block.
What would happen if I instead used public synchronized void add() and public synchronized void doSomething()?
Assuming these are the only places where the list is used, the effect would be the same. But you would syncronize on the object containing the list rather than synchronizing on the list itself.
Basically, all the accesses to a shared state must be synchronized on the same lock. You choose the lock that you prefer. Instead of using a sychronized list, or synchronized methods, you could use a concurrent collection like a CopyOnWriteArrayList.
This should work fine. According to the docs for Collections.synchronizedList, the list mutator methods (add(), etc.) synchronize on the list object itself. Since your iteration loop is also inside a synchronized block, all should be well.
Related
I need to solve this problem. I have console app in which I am inserting some String values. The console app is still running, user can add more and more entries and every 1 minute, there should be another Thread which will print some List statistic (e.g. size etc.). When I use Main Thread for user's console and different Thread for counting and printing this statistic data to the console (both working with the same List). It is enough to use:
List<String> list = Collections.synchronizedList(new ArrayList<>());
Or I need to user volatile and access the List in the synchronized block as well?
Thank you!
It depends what you want to do with the list.
Collections.synchronizedList(...) simply wraps every method with a synchronized block, synchronizing on itself. The synchronization starts when the invoked method starts executing, and stops when it stops executing.
Notionally, it the list is wrapped like this:
class SynchronizedList<T> implements List<T> {
private List<T> delegate;
#Override public int size() {
synchronized (this) {
return delegate.size();
}
}
// ...
}
As such, doing simple things like list.size(), list.add(...), list.get(...) don't require any further synchronization.
However, if you want to do more complex things, for example iterating the list, yes, you need additional synchronization:
synchronized (list) {
for (String s : list) { ... }
}
This is because the synchronization stops as soon as the hidden call to list.iterator() completes; hence, other threads would be able to modify the list while you are iterating.
An object has two synchronized list attributes (listA and listB) and a method called add(...) which adds an element into these two lists. My question is: should this method be synchronized if it is called from differnt threads?
can problems occur when the method SwingUtilities.invokeLater is used inside synchronozed methods?
when using synchronized(this){..code...}, is in this case only the code-block or all methods of the object (this) blocked when a thread access the code-block?
Actually synchronized methods are the same as synchronized(this){} because "this" refers to the object and this one will provide the lock. If you use any of these synchronizations and one thread calls a synchronized method, the other threads cannot access all synchronized methods of the object. if you want to avoid this, create a new variable to provide the lock instead of synchronizing the method. For instance
public void add(String s)
{
private Object block = new Object();
synchronized(block)
{
listA.add(s);
listB.add(s);
}
}
public int count()
{
return listA.size(); //returns the most recent listA update size
// method can be accessed while another thread adds elements to listA
}
An object has two synchronized list attributes (listA and listB) and a
method called add(...) which adds an element into these two lists. My
question is: should this method be synchronized if it is called from
differnt threads?
That depends. If you want the order of the elements in listA and listB to be the same, then yes, you should synchronize when you're adding to both lists. Otherwise, if would be possible that you get things in this order:
Thread A adds element to listA
Thread B adds element to listA
Thread B adds element to listB
Thread A adds element to listB
-
can problems occur when the method SwingUtilities.invokeLater is used
inside synchronozed methods?
No, there's no problem with that. I don't really see what problem you think that you could get.
when using synchronized(this){..code...}, is in this case only the
code-block or all methods of the object (this) blocked when a thread
access the code-block?
It blocks all non-static methods that are synchronized (because that means the same as synchronized (this) and all code blocks that use synchronized (this).
In addition, it blocks any code in other classes that synchronizes on the same object.
Static methods and non-synchronized methods do not block while you are inside synchronized (this).
Updated the question.. please check secodn part of question
I need to build up a master list of book ids. I have multiple threaded tasks which brings up a subset of book ids. As soon as each task execution is completed, I need to add them to the super list of book ids. Hence I am planning to pass below aggregator class instance to all of my execution tasks and have them call the updateBookIds() method. To ensure it's thread safe, I have kept the addAll code in synchronized block.
Can any one suggest is this same as Synchronized list? Can I just say Collections.newSynchronizedList and call addAll to that list from all thread tasks? Please clarify.
public class SynchronizedBookIdsAggregator {
private List<String> bookIds;
public SynchronizedBookIdsAggregator(){
bookIds = new ArrayList<String>();
}
public void updateBookIds(List<String> ids){
synchronized (this) {
bookIds.addAll(ids);
}
}
public List<String> getBookIds() {
return bookIds;
}
public void setBookIds(List<String> bookIds) {
this.bookIds = bookIds;
}
}
Thanks,
Harish
Second Approach
So after below discussions, I am currently planning to go with below approach. Please let me know if I am doing anything wrong here:-
public class BooksManager{
private static Logger logger = LoggerFactory.getLogger();
private List<String> fetchMasterListOfBookIds(){
List<String> masterBookIds = Collections.synchronizedList(new ArrayList<String>());
List<String> libraryCodes = getAllLibraries();
ExecutorService libraryBookIdsExecutor = Executors.newFixedThreadPool(BookManagerConstants.LIBRARY_BOOK_IDS_EXECUTOR_POOL_SIZE);
for(String libraryCode : libraryCodes){
LibraryBookIdsCollectionTask libraryTask = new LibraryBookIdsCollectionTask(libraryCode, masterBookIds);
libraryBookIdsExecutor.execute(libraryTask);
}
libraryBookIdsExecutor.shutdown();
//Now the fetching of master list is complete.
//So I will just continue my processing of the master list
}
}
public class LibraryBookIdsCollectionTask implements Runnable {
private String libraryCode;
private List<String> masterBookIds;
public LibraryBookIdsCollectionTask(String libraryCode,List<String> masterBookIds){
this.libraryCode = libraryCode;
this.masterBookIds = masterBookIds;
}
public void run(){
List<String> bookids = new ArrayList<String>();//TODO get this list from iconnect call
synchronized (masterBookIds) {
masterBookIds.addAll(bookids);
}
}
}
Thanks,
Harish
Can I just say Collections.newSynchronizedList and call addAll to that list from all thread tasks?
If you're referring to Collections.synchronizedList, then yes, that would work fine. That will give you a object that implements the List interface where all of the methods from that interface are synchronized, including addAll.
Consider sticking with what you have, though, since it's arguably a cleaner design. If you pass the raw List to your tasks, then they get access to all of the methods on that interface, whereas all they really need to know is that there's an addAll method. Using your SynchronizedBookIdsAggregator keeps your tasks decoupled from design dependence on the List interface, and removes the temptation for them to call something other than addAll.
In cases like this, I tend to look for a Sink interface of some sort, but there never seems to be one around when I need it...
The code you have implemented does not create a synchronization point for someone who accesses the list via getBookIds(), which means they could see inconsistent data. Furthermore, someone who has retrieved the list via getBookIds() must perform external synchronization before accessing the list. Your question also doesn't show how you are actually using the SynchronizedBookIdsAggregator class, which leaves us with not enough information to fully answer your question.
Below would be a safer version of the class:
public class SynchronizedBookIdsAggregator {
private List<String> bookIds;
public SynchronizedBookIdsAggregator() {
bookIds = new ArrayList<String>();
}
public void updateBookIds(List<String> ids){
synchronized (this) {
bookIds.addAll(ids);
}
}
public List<String> getBookIds() {
// synchronized here for memory visibility of the bookIds field
synchronized(this) {
return bookIds;
}
}
public void setBookIds(List<String> bookIds) {
// synchronized here for memory visibility of the bookIds field
synchronized(this) {
this.bookIds = bookIds;
}
}
}
As alluded to earlier, the above code still has a potential problem with some thread accessing the ArrayList after it has been retrieved by getBookIds(). Since the ArrayList itself is not synchronized, accessing it after retrieving it should be synchronized on the chosen guard object:
public class SomeOtherClass {
public void run() {
SynchronizedBookIdsAggregator aggregator = getAggregator();
List<String> bookIds = aggregator.getBookIds();
// Access to the bookIds list must happen while synchronized on the
// chosen guard object -- in this case, aggregator
synchronized(aggregator) {
<work with the bookIds list>
}
}
}
I can imagine using Collections.newSynchronizedList as part of the design of this aggregator, but it is not a panacea. Concurrency design really requires an understanding of the underlying concerns, more than "picking the right tool / collection for the job" (although the latter is not unimportant).
Another potential option to look at is CopyOnWriteArrayList.
As skaffman alluded to, it might be better to not allow direct access to the bookIds list at all (e.g., remove the getter and setter). If you enforce that all access to the list must run through methods written in SynchronizedBookIdsAggregator, then SynchronizedBookIdsAggregator can enforce all concurrency control of the list. As my answer above indicates, allowing consumers of the aggregator to use a "getter" to get the list creates a problem for the user of that list: to write correct code they must have knowledge of the synchronization strategy / guard object, and furthermore they must also use that knowledge to actively synchronize externally and correctly.
Regarding your second approach. What you have shown looks technically correct (good!).
But, presumably you are going to read from masterBookIds at some point, too? And you don't show or describe that part of the program! So when you start thinking about when and how you are going to read masterBookIds (i.e. the return value of fetchMasterListOfBookIds()), just remember to consider concurrency concerns there too! :)
If you make sure all tasks/worker threads have finished before you start reading masterBookIds, you shouldn't have to do anything special.
But, at least in the code you have shown, you aren't ensuring that.
Note that libraryBookIdsExecutor.shutdown() returns immediately. So if you start using the masterBookIds list immediately after fetchMasterListOfBookIds() returns, you will be reading masterBookIds while your worker threads are actively writing data to it, and this entails some extra considerations.
Maybe this is what you want -- maybe you want to read the collection while it is being written to, to show realtime results or something. But then you must consider synchronizing properly on the collection if you want to iterate over it while it is being written to.
If you would just like to make sure all writes to masterBookIds by worker threads have completed before fetchMasterListOfBookIds() returns, you could use ExecutorService.awaitTermination (in combination with .shutdown(), which you are already calling).
Collections.SynchronizedList (which is the wrapper type you'd get) would synchronize almost every method on either itself or a mutex object you pass to the constructor (or Collections.synchronizedList(...) ). Thus it would basically be the same as your approach.
All the methods called using the wrapper returned by Collections.synchronizedList() will be synchronized. This means that the addAll method of normal List when called by this wrapper will be something like this :-
synchronized public static <T> boolean addAll(Collection<? super T> c, T... elements)
So, every method call for the list (using the reference returned and not the original reference) will be synchronized.
However, there is no synchronization between different method calls.
Consider following code snippet :-
List<String> l = Collections.synchronizedList(new ArrayList<String>);
l.add("Hello");
l.add("World");
While multiple threads are accessing the same code, it is quite possible that after Thread A has added "Hello", Thread B will start and again add "Hello" and "World" both to list and then Thread A resumes. So, list would have ["hello", "hello", "world", "world"] instead of ["hello", "world", hello", "world"] as was expected. This is just an example to show that list is not thread-safe between different method calls of the list. If we want the above code to have desired result, then it should be inside synchronized block with lock on list (or this).
However, with your design there is only one method call. SO IT IS SAME AS USING Collections.synchronizedList().
Moreover, as Mike Clark rightly pointed out, you should also synchronized getBookIds() and setBookIds(). And synchronizing it over List itself would be more clear since it is like locking the list before operating on it and unlocking it after operating. So that nothing in-between can use the List.
I'm trying to improve my understanding of the scope of the lock issued during a synchronized call.
Eg:
class CopyOnReadList<T> {
private final List<T> items = new ArrayList<T>();
public void add(T item) {
items.add(item);
}
public List<T> makeSnapshot() {
List<T> copy = new ArrayList<T>();
synchronized (items) {
// Make a copy while holding the lock.
for (T t : items) copy.add(t);
}
return copy;
}
}
(Code lovingly borrowed from this excellent answer)
In this code snippet, can one thread call add while another is calling makeSnapshot?. Ie., does the lock created by synchronized (items) affect all attempted reads to items, or only those attempted through the makeSnapshot() method?
The original post actually used a synchonized lock in the add method:
public void add(T item) {
synchronized (items) {
// Add item while holding the lock.
items.add(item);
}
}
What is the side effect of removing this?
It affects only those attempted in makeSnapshot() or, more generally, any other method that has synchronized(items) block (it means that it will try to aquire lock on items object and block until it's possible).
The side effect of removing synchronized block from add() method is that add() will not try to synchronize on items object, and therefore will allow concurrent modifications, including while makeSnapshot() is executing.
Without synchronize in add() you can have other threads add elements to items collection WHILE the snapshot is being made.
In this code snippet, can one thread call add while another is calling
makeSnapshot?
Certainly - and either one of the methods can then fail with a ConcurrentModificationException, or the content of the list may be corrupted.
does the lock created by synchronized (items) affect all attempted
reads to items, or only those attempted through the makeSnapshot()
method?
Neither. The lock has not effect whatsoever on the behaviour of the object items, only on blocks or methods that snychronize on it - namely to ensure that no two threads can execute any of those blocks or methods at the same time.
can one thread call add while another is calling makeSnapshot?
Yes. synchronized makes sure that any other thread can't enter another block of code which is also synchronized, on the same object (the CopyOnReadList, in this case). Since you have not synchronized the add method, several threads can call add concurrently, even if one thread is executing makeSnapshot.
By removing the synchronized on the add method, you've made the code non-threadsafe, since ArrayList is not thread-safe.
The rule of thumb is: every access (read or write) to a shared mutable state must be synchronized on the same lock.
This is my first time using the synchronized keyword, so I am still unsure of how it exactly works. I have a list that I want to be accessed by multiple threads so I do this:
players = Collections.synchronizedList(new ArrayList<Player>(maxPlayers));
Now, I want to make sure that I am not calling players.add() at the same time as players.get(), so I think i should use synchronized statements (methods A and B could be called at the same time):
public void A() {
synchronized(players) {
players.add(new Player());
}
}
public void B(String msg) {
synchronized(players) {
for(int i = 0;i<players.size();i++) {
players.get(i).out.println(msg);
}
}
}
Is this the correct procedure? If not, what should I do instead?
Provided you only access the list through the object returned by synchronizedList then access should be thread-safe, though note that you may need to used synchronized blocks for compound actions like iterating through the list or making actions and decisions based on multiple calls into the list (for example, getting a value making a decision then adding a value).
So in your example A() doesn't need the synchronized block, but B() might if you don't want the list to be changed or be read by some other thread during the iteration. (In fact, by using the counter to iterate it is needed to prevent a race condition between the loop termination condition and another thread removing an item; other ways of iterating might not have this issue though).