3SFE605 Concurrent Programming 2008
3SFE605 Concurrent Programming 2008
3SFE605 Concurrent Programming 2008
School of Informatics
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
(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]
(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]
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]
(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:
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:
(c) Assuming that the following classes have already been defined:
(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
(b) Write a Java class that operates as a general semaphore. The semaphore
class’ “constructor” should have two arguments:
(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.
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.
[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 }
[Continued Overleaf]
c University of Westminster 2008
MODULE CODE: 3SFE605 Page 9 of 9
MODULE TITLE: Concurrent Programming
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