• Keine Ergebnisse gefunden

Exercise3SynchronizationandSharedObjects Exercise2Multi-threadedChatsystem Exercise1TheJavaCollectionsFrameworkandThreadSafety PracticeSheet10(HintsandComments) AdvancedAspectsofObject-OrientedProgramming(SS2013)

N/A
N/A
Protected

Academic year: 2022

Aktie "Exercise3SynchronizationandSharedObjects Exercise2Multi-threadedChatsystem Exercise1TheJavaCollectionsFrameworkandThreadSafety PracticeSheet10(HintsandComments) AdvancedAspectsofObject-OrientedProgramming(SS2013)"

Copied!
2
0
0

Wird geladen.... (Jetzt Volltext ansehen)

Volltext

(1)

Prof. Dr. A. Poetzsch-Heffter Mathias Weber, M.Sc.

University of Kaiserslautern Department of Computer Science Software Technology Group

Advanced Aspects of Object-Oriented Programming (SS 2013) Practice Sheet 10 (Hints and Comments)

Exercise 1 The Java Collections Framework and Thread Safety

a) An object ( a component, a class,...) is thread-safe, if it behaves according to its specification when executed in a multi-threaded program context. A different possible definition is: An implementation is thread-safe if it is guaranteed to be free of race conditions when accessed by multiple threads simultaneously.

b) They synchronize on the wrapper object and delegate the execution of the calls to the wrapped object. This guarantees, that if all accesses to the wrapped collection are done via the wrapper, the collection is thread-safe. If wrapped the object is directly accessed, the behavior is unspecified.

c) The thread executing the code may get interrupted after the call tocontainsKey, another thread can now modify m and when the first thread continues, his assumption about the state of m is false. Solution: synchronize on m, for the parts where you need exclusive access to m.

Map < String , String > m = Collections . synchronizedMap (new HashMap < String , String >(...));

synchronized(m) { m. put ("a" , "b" );

if !( m. containsKey ("a" )) { m. put ("c" , "d" );

} }

d) The ConcurrentHashMap allows simultanous reads from the map, reading may also overlap writing, but it is ensured, that a read returns only results from writes that have been completed. The wrapper serializes all access to the underlying map. TheConcurrentHashMapneeds less external synchronization in many cases, but in the same time, the results of read operations get even more unpredictable, additionaly the behavior of iterators over the map changed.

e) Same problem as above. But now multiple thread can be concurrently in the Map implementation, in order to guar- antee mutual exclusive access for a thread, we have to protect all possible concurrent accesses with a synchronize block.

Exercise 2 Multi-threaded Chatsystem

See provided sources. The implementation is one possibility to implement a chat server, which accepts clients and does not block the chat if a lengthy operation is executed for one client (part b). However, it does not handle the possible exceptions in a nice way, because exceptions may lead to arbitrary behavior. Note, that both implementations guarantee that all clients see the messages in the same order, because all writing to the stream (or to the queue) is ordered by the synchronization on the sessions-object.

Exercise 3 Synchronization and Shared Objects

The only shared variable is the field m in class D. It is shared between threads t3 and t4. The implementation of D is problematic because it depends on the scheduler which of the locally created List of which thread remains local and which gets shared, this may be unwanted, but nevertheless the implementations are thread-safe. All accesses to the lists are correctly synchronized.

C can be completely unsynchronized. The local list can never be accessed by more than one thread and even if generateObject is called concurrently this does not cause any problems.

The local list of D may be accessed by more than one thread, the synchronization in run are needed. ThegenerateObject method has to be synchronized, otherwise the field m may be modified by the other thread between the test and the set. The synchronization onthis.mis needed because it may be the shared list and can be modified on by the other

(2)

threads run method. Because all accesses to the lists are guarded by a corresponding synchronized block, the lists itself do not have to be created with the synchronization wrapper. All other synchronization cannot be removed.

It is possible to achieve thread-safety in this example by synchronizing on different objects and at different places. So, the given version is not the only one.

2

Referenzen

ÄHNLICHE DOKUMENTE

a) Implement a method IPlugin load(String clazz) that loads the plugin with the given name. Please also check that the loaded class really implements the needed interface.. b)

Does the print method in class PhDAssistant override the print method in class Person ? Hint: look at the Java Language Specification in Section 8.4.8... Exercise

Collection&lt;E&gt; c = ... Write a generic class TransformingIterator and the accompanying interface Transformer , that decorates an existing Iterator with a transformation

a) Model fields form a model state, which can be different from the implemented state.. The model state may be better suited for specifying the behavior of an object, because it can

Since the example program neither uses synchronized blocks nor volatile fields, the value the main threads sets is never synchronized with the background thread. The example can

The following shows you a sample session.. a) Modify server and client, such that the server can handle multiple clients at the same time. All messages shall be displayed immediately

in JCoBox it is guaranteed, that if an object a of CoBox a sends two messages to objects in another CoBox, these messages will be handled in the same order as they have been send,

RMI is directly supported by the language and uses real java-objects as parameters and message receivers. With some exceptions they behave like standard objects, whereas a