Book Web Page for
Concurrent and Real-Time Programming in
Java
Andy Wellings
Published by Wiley, 2004
ISBN 0-470-84437-X
431 pages |
Table
of Contents
1. Introduction
2. Concurrent Programming in Java
3. Communication and Synchronization
4. Completing the Java Concurrency Model
5, Implementing Communications Paradigms in Java
6. Case Study: Concurrent Maze Sorter
7. The Real-Time Specification for Java
8. Memory Management
9. Clocks and Time
10. Scheduling and Schedulable Objects
11. Asynchronous Events and their Handlers
12. Real-Time Threads
13. Asynchronous Transfer of Control
14. Resource Sharing
15. Physical and Raw Memory
16. Case Study: Automobile Cruise Contriol System
17. High-Integrity Real-Time Systems
18. Conclusions
|
Web References
- The current version of the Real-time Specification for Java
can
be found at the
www.rtsj.org .
RTSJ Software
There are several implementations of the RTSJ:
Other real-time, Java-like platforms:
Warning: The RTSJ is still under
refinement and the implementations are still under development.
Consequently, the various implementations may all do slightly
different
things.
Book Evaluation
This is the first edition of the book. If you have any comments or
find
any errors please email me Andy.Wellings@york.ac.uk
There is an errata
page with the current list of typos etc.
Support Material
The book assumes the reader is familiar with sequential Java and
the
basic tennets of software engineering. A brief introduction to
Java
(currently 1.4) is provided for those people
familiar
with C++/C and OOP in general. Introduction
in
pdf
form
Foils
Currently I teach the main parts of the book in 20 one hour
lectures.
Copies of the power point presentations I use are given
below:
- Lecture 2 - Concurrent Programming in Java (power
point)
- Lecture 3 - Communication and Synchronization Part 1 ( power
point )
- Lecture 4 - Communication and Synchronization Part 2 ( power
point )
- Lecture 6 - The Real-time Specification for Java Part 1 ( power
point )
- Lecture 7 - The Real-time Specification for Java Part 2 ( power
point )
- Lecture 12 - Scheduling and Schedulable Objects (power
point)
- Lecture 13 - Scheduling and Schedulable Objects (power
point)
- Lecture 14 - Asynchronous Events and their Handlers (power
point)
- Lecture 16 - Asynchronous Transfer of Control Part 1 (power
point)
- Lecture 17 - Asynchronous Transfer of Control Part 2 (power
point)
Example Program
Here are some example programs:
Exercises
Here are some exercises to help reinforce the material presented
in
the book.
Concurrent Hello World (Easy)
- Write a Java class which is a subclass of the Thread class.
The
run method of the class should print the message
"hello
world". Write a main Java method which creates the
thread
and starts it..
- Write a class which implements the Runnable interface.
The
run method of the class should print the message
"hello
world". Write a main method which creates the thread and
passes
the Runnable object as a parameter and starts the
thread.
Java Thread Scheduling (Easy)
- Write a concurrent Java program which consists of two
threads.
The first thread prints the numbers 1 to 100. The second thread
prints
the
letters a to z followed by A to Z. Start both threads.
Understand what
is
happening.
- Modify 1) so that the second thread wait for the first thread
to
terminate before it begins outputting.
- Modify 1) so that each thread sleeps for one second after
printing a number (or a letter).
Thread Local Data (More difficult)
- Read up about Thread Local data. Flesh out the example in
Section 2.5 and test it.
- Experiment to determine the difference between static and non
static thread local data.
Accessing Shared Data (Easy)
- Write a concurrent Java program which declares an array
of
10 integers. Create two threads. One which writes the value 1 to
all
the elements in the array. However, in between writing each
element of
the array, the thread sleeps for one second. The second thread
writes
the values 7
to each element of the array. Again in between writing the
elements it
sleeps. When both threads have terminated, print out the array.
Understand what
is happening.
- Now modify the program so that, even with the sleeps, the
result
at the end of the threads is (1,1,1,1,1,1,1,1) or
(7,7,7,7,7,7,7,7,7,7). You will need to use a synchronized
method or a
synchronized statement to achieve this.
Readers Writers (More difficult)
- Provide a solution to the readers/writers problem which gives
priority to waiting readers. Use a single class solution (i.e.
do not
use condition variables).
The Santa Claus Problem (Difficult - designed by J.A. Trono)
Santa Claus sleeps at the North pole until awakened by either all of
the nine reindeer, or by a group of three out of ten elves. He
performs
one of two indivisible actions:
- If awakened by the group of reindeer, Santa harnesses them to
a sleigh, delivers toys, and finally unharnesses the reindeer
who then
go on vacation.
- If awakened by a group of elves, Santa shows them into his
office, consults with them on toy R&D, and finally shows
them out
so they can return to work constructing toys.
A waiting group of reindeer must be served by Santa before a waiting
group of elves. Since Santa's time is extremely valuable,
marshalling
the reindeer or elves into a group must not be done by Santa. Show
how
this problem can be modelled in standard Java. Santa, the
elves
and
the reindeer are all threads that must synchronize with each other.
(You
can find the answer to this on the web, but try it first).
Accessing Memory Areas
- Write a program which tries to allow a standard Java thread
to
enter into the immortal memory area. Tell me what happens!
(Easy)
- Write a program which tries to create a standard Java thread
in the immortal memory area. Again, tell me what happens.
(Easy)
- Write a program which tries to create a standard Java thread
in a scoped memory area. Again, tell me what happens.
(Easy)
- Use the SizeEstimator class to determine the size of various
object, including an object of class Object. (Easy)
After we have covered real-time threads:
- Try to use some of the standard Java packages for input and
output from within a scoped memory area (Easy)
- Flesh out the example in Section 7 of the draft book on
Portals
and test it. (More difficult).
- Using MemoryParameters and determine whether the Reference
Implementation reports violations of the memory usage (More
difficult).
Clocks and Time
- Write a program to determine the granularity of time for the
reference implementation on Linux. (Easy)
- Write a program to test the Launch Clock given in the
Lectures
and Chapter 9 of the draft book. (More difficult)
Scheduling and Schedulable Objects
- Create 6 periodic real-time threads each with a utilization of
20% (that is the cost/period = .2). Ask the default scheduler
whether
the system is schedulable? Understand why you get the answer you
do.
What feasibility test is the Reference Implementation
using?
(Easy)
- Modify the program so that it dynamically changes the value of
the cost parameters until they are all the same and
the
system is feasible. (More difficult)
- Write a program to test if the Reference Implementation
supports cost overrun handlers. (Easy)
Asynchronous Events and Real-Time Threads
- Write programe to test whether the Reference Implementation
supports deadline overrun handlers. (Easy)
- Write a program to explore the semantics of the waitForNextPeriod
method. Does the Reference Implementatio implement the
semantics given in the course? (More difficult).
- Write a real-time thread which is periodic and has a deadline
overrun handler. However, do not use waitForNextPeriod and
do
not set the RTSJ deadline miss handler. In other words, show how
you
can implement a periodic real-time threads using Timers.
(Moderately difficult).
- Determine whether the Reference Implementation terminates a
programme even if events can still fire.(Easy).
- Experiement with the cade given for an aperiodic real-time
thread
in section 12.3 of the draft book. Report any problems to me!
(More
difficult).
Asynchronous Transfer of Control
- Write a program with two real-time threads, one enters into a
AI-method which loops forever (say priniting out hello world).
The
other sleeps for 5 seconds and then calls the interrupt method
on the
first thread.Ensure that the first thread handles any AIE and
prints
out a message. (More difficult).
- Re-do 1) above using AIE objects and the interruptible
interface.
(Easy).
- Write programs which implement the method call chains
illustrated
in section 13.3. of the draft book. Run the programs and check
the
results given in the draft book. Tell me if they are not the
same!
(More difficult).
- Implement and test the Persistent AIE given in section 13.5 of
the draft book (More difficult).
- Devise a program to illustrate the semantics of multiple AIEs
(section 13.6 of the draft book) (Difficult).
- Write a real-time thread that enters an infinite loop. Write
the
deadline miss handler so that it terminates the thread
(gracefully,
i.e. the thread itself should report that has missed its
deadline (not
the handler) before terminating). (More difficult)
- Test the impreciase computation examle given in sectio 13.7 of
the draft book. (More difficult).
- Does the Reference Implementation support the POSIX Sign al
interface? Try and write a program that catches control-C.
(Difficult).
Resource Sharing
- Write a program to illustrate priority inheritance (More
difficult).
Example Examination Questions
Here are some
example questions that I use to examine the material in the book.
Example Question on Java Thread Model
- Summarize the strengths and weaknesses of the Java
Concurrency model
- Consider the following class
public class Resource
{
// There should never be threads executing methodA
and
methodB
// at the same time.
// Calls to methodA have priority over calls to
methodB.
public void methodA()
{
// No more than three threads should be
allowed to
// execute this method at any one time.
// The method takes a significant period
of
time to execute.
}
public void methodB()
{
// No more than two threads should be
allowed
to
// execute this method at any one time.
// The method takes a significant period
of
time to execute.
}
}
Write a class called Controller which acts as an front end to
the
Resource class. The Controller should enforce the access
contraints on
methodA
and methodB. Show how the Controller can be used.
Example Question on Memory Management
- Explain the approach to memory management in the RTSJ (13
marks).
- Consider the following parent real-time thread's scoped
memory
stack:
The parent creates a new schedulable object with an initial
memory
area of the heap. Draw the new schedulable objects scoped memory
stack. (3 marks)
- Consider the following parent real-time thread's scoped
memory
stack:
The parent creates a new schedulable object with an initial
memory
area of scopedE. Draw the new schedulable objects scoped memory
stack. (3 marks)
- Consider the following parent real-time thread's scoped
memory
stack:
The parent creates a new schedulable object with an initial
memory
area of the heap. Draw the new schedulable objects scoped memory
stack. (3 marks)
- Consider the following parent real-time thread's scoped
memory
stack:
The parent creates a new schedulable object with an initial
memory
area of scopeE. Draw the new schedulable objects scoped memory
stack.
(3 marks)
Example Question on Asynchronous Transfer of Control plus
Asynchronous Event Handler
- Explain briefly the RTSJ's models of asynchronous transfer
of
control and asynchronous event handler (12 marks)
- Three tug-boats are towing a disabled tanker carrying toxic
chemicals. Each tug-boat is connected to the tanker via
an
intelligent steel cable. A supervisor ship has an
on-board
computer which monitors the stress on the steel cables and
sends
control signals to the on-board computers on the three tugs
(via
wireless communication devices). If
one of the cables is in danger of breaking, it is
necessary to
instruct all tug-boats to immediately release their cables.
The control software on the supervisor ship executes
on
a single processor and is written in Real-Time Java. An
interrupt
is generated when the stress on one of the cables is near
breaking point.
The Java Virtual Machine associates the string
"BreakingLimit"
with
this interrupt. The control software contains the
following class
declaration for controlling the tug-boats:
.
import javax.realtime.*;
public class TugControl
implements
Interruptible
{
public
TugControl(int tid)
{
// tid
is
a Tug identifier
}
private void
computeAndSendTugCableTension()
throws
AsynchronouslyInterruptedException
{
//
computationally complex calculations
// send
communication to appropriate tug's on-board computer
}
private void
sendEmergencyRelease()
{
// send a
emergency command to the Tug to release the cable
}
public void
run(AsynchronouslyInterruptedException exception)
throws
AsynchronouslyInterruptedException
{
computeAndSendTugCableTension();
}
public void
interruptAction(
AsynchronouslyInterruptedException
exception)
{
sendEmergencyRelease();
}
}
Each tug is represented in the control software by a Real-time
Thread
import javax.realtime.*;
public class TugThread extends
RealtimeThread
{
TugControl myTug;
AsynchronouslyInterruptedException myAIE;
public TugThread(TugControl
tc,
AsynchronouslyInterruptedException
ae)
{
super();
myTug =
tc;
myAIE =
ae;
}
public void
run()
{
//
you
fill in
}
}
Show how the above declaration can be used so that when the
interrupt arrives, all TugControl
software immediately release their cables. As well as
completing
the run method in TugThread,
you will need to
sketch the implementation of an asynchronous event handler to
handle
the
"Breaking Limit" interrupt, and show how this can be linked to
the TugThread and TugControl software by a
main
method.You may ignore all issues of thread scheduling in your
answer.
(13 marks)
Old Examination Papers and Specimen Answers
Here are some old examination papers.