Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                

3SFE605 Concurrent Programming 2008

Download as pdf or txt
Download as pdf or txt
You are on page 1of 10

CAVENDISH CAMPUS

School of Informatics

Modular Undergraduate Programme


First Semester 2007 – 2008

Module Code: 3SFE605

Module Title: Concurrent Programming

Date: 21st May 2008

Time: 14:00 – 16:00

Instructions to Candidates:
Answer THREE questions.
Each question is worth 33 marks.


c University of Westminster 2008
MODULE CODE: 3SFE605 Page 1 of 9
MODULE TITLE: Concurrent Programming

Question 1

(a) In Java concurrent programming is achieved by using threads. Describe


the concept of a thread. [5 marks]

(b) A Java thread throughout its existence is always in one of several thread
states. Describe these thread states for a Java JDK 1.5 (or later version)
thread. In addition, explain how a Java thread changes state and give
Java code fragments that produce the state changes. Your answer should
include a diagram of these states and the transitions between them. [20 marks]

(c) Briefly describe the Java scheduling algorithm, and what rôle thread priority
plays in this. Further, explain how the Java scheduler would schedule the
three racer threads defined in the program given in Appendix A. Finally,
give an example of the output that the program could produce. [8 marks]

Question 2

(a) Briefly describe the main features of the concurrent programming language
mechanism known as a monitor, as described by C.A.R. Hoare in his 1974
paper. [5 marks]

(b) Describe in detail Java’s implementation of the monitor mechanism. Your


answer should be illustrated by fragments of Java code. [16 marks]

(c) Describe the main differences between the “classic” monitor mechanism as
presented by C.A.R. Hoare and Java’s version of the monitor mechanism.
[5 marks]

(d) In Appendix B there is a Java program which provides a simple simulation


of sending a SMS text message to a mobile phone.
With reference to this program describe in detail the sequence of states of
the object suesphone and the threads jim and sue during its execution;
assuming that sue calls suesphone’s readtext() method before jim calls
its sendtext() method. [7 marks]


c University of Westminster 2008
MODULE CODE: 3SFE605 Page 2 of 9
MODULE TITLE: Concurrent Programming

Question 3

(a) Give a brief definition of the concept of a design pattern. How do design
patterns improve code reuse and address the problem of “hacking” in the
development of concurrent object-oriented programs? [7 marks]

(b) The Java programming language provides the “interface” facility.


Briefly, explain what this is and how it facilitates the develop of software
using design patterns. [7 marks]

(c) What is the general aim of the safety preservation design patterns for
ensuring a safe concurrent object-oriented program. Explain how the Full
Synchronization safety design pattern attempts to achieve this aim, by
describing its four design steps. [9 marks]

(d) Apply the Full Synchronization design pattern, with “balking”, to produce
a Java BalkingStack class which represents a stack of Objects. Its
requirements are:

• A stack of objects, the maxsize being specified as a parameter of


the constructor.
• A push(item) method which pushes an object onto the stack, if the
stack is full then the method “balks”.
• A pop() method which removes the object at the top of the stack
and returns it, if the stack is empty then the method “balks”.
[10 marks]


c University of Westminster 2008
MODULE CODE: 3SFE605 Page 3 of 9
MODULE TITLE: Concurrent Programming

Question 4

(a) What type of systems are flow design patterns applicable to? Give two
examples of these types of systems. [5 marks]

(b) Describe the following types of components that are used in flow design
patterns:

(i) Representational Components [8 marks]


(ii) Transformational Components [5 marks]
(iii) Coordination Components [3 marks]

(c) Assuming that the following classes have already been defined:

• ProducerStage – the class of the producer.


• ConsumerStage – the class of the consumer, which has a put(item)
method, used to push data to the consumer.
• Item – the class of items placed into the buffers.
• Buffer – the class which provides the underlying buffer data struc-
ture, and has public methods put and take.

(i) Using the above classes, construct a Put-Only Buffer class that uses
a thread to push data to a consumer stage. [8 marks]
(ii) Describe the sequence of method calls involved in an item of data
flowing from a Producer stage via the Put-Only Buffer to a Con-
sumer stage. [4 marks]


c University of Westminster 2008
MODULE CODE: 3SFE605 Page 4 of 9
MODULE TITLE: Concurrent Programming

Question 5

(a) Describe the features of the concurrent programming mechanism intro-


duced by E. W. Dijkstra in the late 1960s, known as a semaphore. [5 marks]

(b) Write a Java class that operates as a general semaphore. The semaphore
class’ “constructor” should have two arguments:

• the initial value of the semaphore; and


• the upper limit of the semaphore.
[10 marks]
(c) Give a brief description of the Producer/Consumer problem. What types
of semaphores are required to solve the Producer/Consumer problem. In
addition explain the purpose of each semaphore. [5 marks]

(d) Using your semaphore class modify the version of the Producer/Consumer
program given in Appendix C, so that it is a safe and live solution of the
problem.
Note: you do not have to copy out the whole program, but only those
parts that are sufficient to indicate which parts you have modified; you
may also make use of the line numbers to help you do this. [13 marks]


c University of Westminster 2008
MODULE CODE: 3SFE605 Page 5 of 9
MODULE TITLE: Concurrent Programming

Appendix A
Program Code for Question 1(c)
The program comprises two classes Racer and RaceStarter.

1 class Racer extends Thread


2 {
3 Racer(int id) { super("Racer #" + id) ; }
4
5 public void run()
6 {
7 for ( int i = 1; i < 40; i++ )
8 {
9 if ( i % 10 == 0 )
10 {
11 System.out.println(getName() + ": i = " + i ) ;
12 yield();
13 }
14 }
15 }
16 }
17
18 class RaceStarter
19 {
20 public static void main( String args[] )
21 {
22 Racer racer1 = new Racer(1);
23 Racer racer2 = new Racer(2);
24 Racer racer3 = new Racer(3);
25
26 racer1.setPriority(7);
27
28 racer1.start();
29 racer2.start();
30 racer3.start();
31 }
32 }


c University of Westminster 2008
MODULE CODE: 3SFE605 Page 6 of 9
MODULE TITLE: Concurrent Programming

Appendix B
Program Code for Question 2(d)
The program comprises four classes: Texter, Recipient, MobilePhone and
SMS.

1 class Texter extends Thread


2 {
3 private final MobilePhone friendsphone ;
4
5 public Texter( MobilePhone phone )
6 {
7 friendsphone = phone ;
8 }
9
10 public void run()
11 {
12 friendsphone.sendtext( new String("Spk l8r.") );
13 }
14 }
15
16 class Recipient extends Thread
17 {
18 private final MobilePhone myphone ;
19
20 public Recipient( MobilePhone phone )
21 {
22 myphone = phone ;
23 }
24
25 public void run()
26 {
27 String textmessage = myphone.readtext();
28 }
29 }

[Continued Overleaf]


c University of Westminster 2008
MODULE CODE: 3SFE605 Page 7 of 9
MODULE TITLE: Concurrent Programming

30 class MobilePhone
31 {
32 private String textmessage = null;
33 private boolean got_message = false;
34
35 public synchronized void sendtext (String message)
36 {
37 while ( got_message ) {
38 try {
39 wait();
40 } catch(InterruptedException e){ }
41 }
42 textmessage = message ;
43 got_message = true ;
44 notify();
45 }
46
47 public synchronized String readtext()
48 {
49 while ( !got_message ) {
50 try {
51 wait();
52 } catch(InterruptedException e){ }
53 }
54 got_message = false ;
55 notify() ;
56 return textmessage ;
57 }
58 }
59
60 class SMS
61 {
62 public static void main(String args[]) {
63 MobilePhone suesphone = new MobilePhone();
64 Texter jim = new Texter( suesphone );
65 Recipient sue = new Recipient( suesphone );
66
67 jim.start();
68 sue.start();
69 }
70 }


c University of Westminster 2008
MODULE CODE: 3SFE605 Page 8 of 9
MODULE TITLE: Concurrent Programming

Appendix C
Program Code for Question 5(d)
Consisting of four classes Buffer, Producer, Consumer and
ProducerConsumer.

1 class Buffer
2 {
3 public Object[] buffer = null ;
4 public final int size ;
5 public int in = 0 ;
6 public int out = 0 ;
7
8 public Buffer( int size ) {
9 this.size = size ;
10 buffer = new Object[size] ;
11 }
12 }

13 class Producer extends Thread


14 {
15 private Buffer b = null ;
16
17 public Producer( Buffer buff ) { b = buff ; }
18
19 public void run() {
20 for (int i = 0; i < 10; i++ ) {
21 b.buffer[b.in] = produceItem() ;
22 b.in = (b.in + 1) % b.size ;
23 }
24 }
25
26 private Object produceItem() { // produce item }
27 }

[Continued Overleaf]


c University of Westminster 2008
MODULE CODE: 3SFE605 Page 9 of 9
MODULE TITLE: Concurrent Programming

28 class Consumer extends Thread


29 {
30 private Buffer b = null ;
31 private Object item = null ;
32
33 public Consumer( Buffer buff ) { b = buff ; }
34
35 public void run() {
36 for (int i = 0; i < 10; i++ ) {
37 item = b.buffer[b.out] ;
38 b.buffer[b.out] = null ;
39 b.out = (b.out + 1) % b.size ;
40
41 consumeItem(item);
42 }
43 }
44
45 private void consumeItem(Object item) { // consume item }
46 }

47 class ProducerConsumer
48 {
49 private static final int N = 5 ;
50
51 public static void main( String args[] )
52 {
53 Buffer buffer = new Buffer(N) ;
54
55 Thread p = new Producer(buffer) ;
56 Thread c = new Consumer(buffer) ;
57
58 p.start() ;
59 c.start() ;
60 }
61 }


c University of Westminster 2008

You might also like