• Keine Ergebnisse gefunden

Advanced Aspects of Object-Oriented Programming (SS 2015) Practice Sheet 9

N/A
N/A
Protected

Academic year: 2022

Aktie "Advanced Aspects of Object-Oriented Programming (SS 2015) Practice Sheet 9"

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 9

Date of Issue: 16.06.15

Deadline: 23.06.15 (before the lecture as PDF via E-Mail)

Exercise 1 Behavioral Subtyping

In this exericse we look at theReaderexample found at the end of slide set 5 (specification and checking).

a) Give a detailed explanation why the execution of methodBuffReader.getChar()cannot lead to an

ArrayIndexOutOfBoundsException.

b) Implement aStringReader. First add an interface equivalent to theBlankReaderincluding the specification of the behavior. Refine the behavior of thegetChar()method such that it returns the next character of the string if the position is not at the end and that it returns -1 if the reader is at the end of the string. Second add an implementation ofStringReaderthat fulfills the specification.

c) Add a method void unread(int c) toBuffReader. The method pushes back a single character by copying it to the front of the buffer. After the method returns, the next character to be read is c. Add a specification and an implementation of the method inBuffReader. Is it easily possible to move the method to theReaderinterface instead? How does this change influence the implementation ofStringReader?

Exercise 2 Concurrent Access to Shared State

Read and write operations on fields are atomic except for fields of typelonganddouble(§17.7). Analize the following code (taken and adapted from the book “Effective Java Second Edition” by Joshua Bloch):

package concurrency . mt ;

import java . util . concurrent . TimeUnit ; public class StopThread {

private static boolean stopRequested ;

public static void main ( String [] args ) throws InterruptedException { Thread backgroundThread = new Thread (new Runnable () {

public void run () { int i = 0;

while(! stopRequested ) { i ++;

}

System . out . println (" Thread terminated ." );

} });

backgroundThread . start ();

TimeUnit . SECONDS . sleep (1);

stopRequested = true;

System . out . println (" Program sould terminate now ... " );

} }

The expected behavior is that the program terminates after one second by telling the background thread to stop work- ing. On some machines, this program does not terminate. Explain this unintended behavior. How can the program be fixed?

(2)

Exercise 3 The Java Collections Framework and Thread Safety

a) Explain the term “thread-safe”.

b) Inform yourself about the synchronization wrappers for the collection classes ofjava.util. How do they work and which guarantees are given?

c) Are there problems with the following code, if it is executed in a multi-threaded program context? If yes, explain and fix the issues.

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

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

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

}

d) Java 5 introduced the packagejava.util.concurrent, that contains classes to support common tasks in multi- threaded programs. Consider the classConcurrentHashMap. What is the difference between aConcurrentHashMap and a synchronized Map as returned by the synchronization wrapper? Which are the (dis-)advantages of using a ConcurrentHashMapover a synchronized Map, and vice versa?

e) Are there problems with the following code, if it is executed in a multi-threaded program context? If yes, explain and fix the issues.

ConcurrentHashMap < String , String > m = new ConcurrentHashMap < String , String >(...);

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

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

}

Referenzen

ÄHNLICHE DOKUMENTE

This was partly a consequence of Germany’s strategy of net- worked security (Vernetzte Sicherheit) which sought to prioritize civilian engagement, while keeping Ger- many’s

Proof of this is the establishment of the Pan-German League in Germany which was founded as an immediate conse- quence of conflict between the government and nationalists

In order to improve effectiveness and output legitimacy, we heard proposals to align the UNFCCC process and the COPs more closely with other environmental and social agendas,

Since diastolic dysfunction in anthracycline-induced cardiotoxicity likely precedes systolic dysfunction, elevated BNP levels might also be a more sensitive marker for

b) Implement a StringReader. First add an interface equivalent to the BlankReader including the specification of the behavior. Second add an implementation of StringReader that

CARNEY, INA MANFREDINI (Familientherapeutin, Sozialarbeiterin, Leiterin der la:sf), MANFRED MITTERMAYER (Literaturwissenschaftler, Ludwig Boltzmann Institut für Geschichte und

Session 1 set the scene for the seminar by discussing the economic backdrop that would underpin discussions at the Leaders’ Summit in Brisbane. It was suggested that the

Investigations were short term and applied in their approach, orientated towards the solution of specific queries, in contrast to some (but not all) of the more free- ranging