


Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
This page cannot be seen from the preview
Don't miss anything!



The final exam is cumulative, covering topics from the entire semester. These questions mainly cover topics from the 2nd half of the course. Look at the two midterms and their practice exams for questions on topics covered earlier in the course.
Unsynchronized access to shared data (data races) Significant unresponsiveness due to lock contention Could deadlock Other problems you can identify (not necessarily threading related)
In each case, your answer may depend the interaction of the MessageBroadcast class with other classes. You should write your question as to whether there are any circumstances under which those problems arise. Would your answers be any different if Listener was not a Remote interface? Describe any differences.
import java.util.*; import java.rmi.RemoteException;
public class MessageBroadcast implements Listener { Set listeners = new HashSet(); public synchronized void addListener(Listener l) { listeners.add(l); } public synchronized void removeListener(Listener l) { listeners.remove(l); } public synchronized void message(String s) throws RemoteException { for(Iterator i = listeners.iterator(); i.hasNext(); ) { Listener l = (Listener) i.next(); l.message(s); } } }
interface Listener extends java.rmi.Remote { void message(String s) throws RemoteException; }
If you found any problems, give a new solution that fixes the problems you found and doesn’t introduce any new ones. It is OK if your new solution allows Listeners to receive messages out of order, but clearly state if your new solution has this feature. Java API notes: HashSet’s are unsynchronized. You can create a synchronized Set with
Set listeners = Collection.synchronizedSet(new HashSet())
For both synchronized and unsynchronized sets, if one thread modifies a set while another thread is iterating through an iterator for the set, it is an error. The interface java.util.Set is defined as: public interface Set extends java.util.Collection { public abstract boolean add(java.lang.Object); public abstract boolean addAll(java.util.Collection); public abstract void clear(); public abstract boolean contains(java.lang.Object); public abstract boolean containsAll(java.util.Collection); public abstract boolean isEmpty(); public abstract java.util.Iterator iterator();
public abstract boolean remove(java.lang.Object); public abstract boolean removeAll(java.util.Collection); public abstract boolean retainAll(java.util.Collection); public abstract int size(); public abstract java.lang.Object toArray()[]; public abstract java.lang.Object toArray(java.lang.Object[])[]; }
Answer : The code is adequately synchronized, if perhaps over synchronized. No data races exist in the code.
However, the message(...) function could lead to the system being unresponsive, because it holds onto a lock while sending messages to listeners. If one client is very slow, it will delay other clients and attempts to add or remove listeners.
There is some potential for deadlock. Say that sending a message to a listener cause a chain of events to leads to attempting to register another listener. If those calls occur via RMI, then the call to register another listener could occur in a thread other than the one that broadcast the message, leading to deadlock.
The message(...) does not handle RemoteExceptions well. If a RemoteException occurs in sending a message to any listener, it doesn’t try to contact the other listeners and just immediately propagates the message to it’s caller.
If Listener were not a RemoteInterface, then deadlock would be a little harder to provoke. But if another thread held a lock on a Listener, than then sent a message to a MessageBroadcast that listener was subscribed to, then deadlock could result if another thread was sending messages through that MessageBroadcast.
The basic fix to these problems is to not hold a lock while making calls to listeners. However, if we are iterating through the HashSet while another thread adds or removes listeners, we have a problem. So we make a copy of the set of Listeners we need to send a message to.
public void message(String s) { Object [] l; synchronized (this) { l = listeners.toArray(); } for(int i = 0; i < l.length; i++) { try { ((Listener)l[i]).message(s); } catch (RemoteException e) {} } }
This solution just ignores RemoteExceptions. Alternatively, it could call removeListener when a RemoteExcep- tion is thrown talking to a Listener.
Because this code is unsynchronized, it is possible that if two threads invoke the message function, different clients will receive the messages in different orders, and a particular client will receive messages in an order different than the order of the message() invocations.