• Keine Ergebnisse gefunden

Exercise3Asynchronousvs.synchronousCalls Exercise2SynchronizationandSharedObjects Exercise1Multi-threadedChatsystem PracticeSheet10(HintsandComments) AdvancedAspectsofObject-OrientedProgramming(SS2015)

N/A
N/A
Protected

Academic year: 2022

Aktie "Exercise3Asynchronousvs.synchronousCalls Exercise2SynchronizationandSharedObjects Exercise1Multi-threadedChatsystem PracticeSheet10(HintsandComments) AdvancedAspectsofObject-OrientedProgramming(SS2015)"

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 2015) Practice Sheet 10 (Hints and Comments)

Exercise 1 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 2 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 is 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 threads run method. Because all accesses to the lists are guarded by a corresponding synchronized block, the lists itself does 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.

Exercise 3 Asynchronous vs. synchronous Calls

a) Synchronous calls: all methods are executed in the order they appear in the snippet. Output:

i=5 i=9

Asynchronous calls: it is unknown, when the methods are executed:

i=5 i=9

or, if C handles the message send by B before the one of A

i=9 i=5

b) With synchronous communication methods are executed completely before the control returns to the caller. That means that the order of execution of methods corresponds to the order of the send messages. In asynchronous contexts, the order of messages may change. Depending on the language, certain guarantees about the message order can be given, but in most cases the order will not be total but some partial order. E.g. 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, but the system makes no guarantees about messages send to or by other CoBoxes.

(2)

In multi-threaded contexts the usage of synchronous communication is easier because the behaviour is “easier” to predict. But synchronous communication is a source for deadlocks, which are less likely in asynchronous settings, but for the price of even less predictable behaviour.

2

Referenzen

ÄHNLICHE DOKUMENTE

// invariant : either all components are visible or all are hidden. Capturing occurs if a reference, which was passed as a parameter to a method, is stored in a field of the

– Enforces “Think before you code”. The developer has to think about the precise meaning of the inter- faces he uses, this allows to detect errors and problem quite early in

Starting a method-specification with the keyword also is intended to tell the reader that this specification is in addition to some specifications of the method that are given in

false. This does not guarantee that there exists only a single instance of the field for all threads of the program. In the Java memory model, every thread has its own instance of

In this exercise we want to develop a simple Chatserver, that can be used with telnet as client. You can use the code provided on the webpage as basis for your implementation.

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

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

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