Docsity

Practice Final Questions - Design Patterns, Thread Patterns | CMSC 433, Exams of Programming Languages

Toggle sidebar
bg1
433 Practice Final Questions - Design Patterns, Thread Patterns
The final exam is cumulative, coveringtopics 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.
1. (Threads/RMI/Design Patterns)
Consider the following code for threading and synchronization issues. Comment on whether it can exhibit:
¯
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 isdefined 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[])[];
}
1
pf3
pf4

Partial preview of the text

Download Practice Final Questions - Design Patterns, Thread Patterns | CMSC 433 and more Exams Programming Languages in PDF only on Docsity!

433 Practice Final Questions - Design Patterns, Thread Patterns

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.

  1. (Threads/RMI/Design Patterns) Consider the following code for threading and synchronization issues. Comment on whether it can exhibit:

 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.

  1. (RMI/Design Patterns) Say I want to set up a distributed service, with clients and servers communicating via RMI. I want to allow anyone to write their own client application. However, I don’t want to have the server uploading client stubs for each of the different clients I communicate with, for several reasons: it requires clients to set up a codebase where the stub can be uploaded from, there are security issues with uploading the code to the server (denial of service attacks aren’t prevented by the security model), etc. Instead, I want to have just one client stub, no matter how many client applications the server communicates with. That way, the stub code doesn’t have to be uploaded. Don’t depend on the hack that two different clients with the same class name that implement the same interface can use the same stub (as we did in project 4). Describe a design for allowing servers to send messages to client applications, without requiring any code to be uploaded to the server. There is a good solution that doesn’t require any mucking with rmic (the RMI compiler) or any other details of the RMI implementation. Such a solution is (much) preferred. Your solution may require some changes in how people write and use clients: that is OK. Answer: Define a standard ClientProxy class using the Proxy design pattern. The ClientProxy class is a remote object that implements the Client interface, and takes a reference to another (local) object that implements the Client interface. The ClientProxy simply passes all calls through to the local object. Since the ClientProxy is standard and the only remote object, the server can have a copy of the ClientProxy Stub code already loaded, and doesn’t need to load any code from the clients.