Real time program in java

Book Evaluation

There is an errata page with the current list of typos etc.

Support Material

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 18 — Resource Sharing (power point)
  • Lecture 19 — Feasibility Analysis (power point)
  • Lecture20 — Conclusions (power point)

Example Program

  • The Missile Firingexample taken from section 8.7 of the book.
  • The Launch Clock example taken from section 9.2 of the book
  • The Thruster Control System example taken from section 11.7 of the book.

Exercises

Here are some exercises to help reinforce the material presented in the book.

Concurrent Hello World (Easy)

  1. 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..
  2. 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)

  1. 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.
  2. Modify 1) so that the second thread wait for the first thread to terminate before it begins outputting.
  3. Modify 1) so that each thread sleeps for one second after printing a number (or a letter).
Читайте также:  Java get current class name

Thread Local Data (More difficult)

  1. Read up about Thread Local data. Flesh out the example in Section 2.5 and test it.
  2. Experiment to determine the difference between static and non static thread local data.

Accessing Shared Data (Easy)

  1. 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.
  2. 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)

  1. 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)

  • 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.

Accessing Memory Areas

  1. Write a program which tries to allow a standard Java thread to enter into the immortal memory area. Tell me what happens! (Easy)
  2. Write a program which tries to create a standard Java thread in the immortal memory area. Again, tell me what happens. (Easy)
  3. Write a program which tries to create a standard Java thread in a scoped memory area. Again, tell me what happens. (Easy)
  4. Use the SizeEstimator class to determine the size of various object, including an object of class Object. (Easy)
  1. Try to use some of the standard Java packages for input and output from within a scoped memory area (Easy)
  2. Flesh out the example in Section 7 of the draft book on Portals and test it. (More difficult).
  3. Using MemoryParameters and determine whether the Reference Implementation reports violations of the memory usage (More difficult).
Читайте также:  Java spring телеграм бот

Clocks and Time

  1. Write a program to determine the granularity of time for the reference implementation on Linux. (Easy)
  2. 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

  1. 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)
  2. 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)
  3. Write a program to test if the Reference Implementation supports cost overrun handlers. (Easy)

Asynchronous Events and Real-Time Threads

  1. Write programe to test whether the Reference Implementation supports deadline overrun handlers. (Easy)
  2. Write a program to explore the semantics of the waitForNextPeriod method. Does the Reference Implementatio implement the semantics given in the course? (More difficult).
  3. 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).
  4. Determine whether the Reference Implementation terminates a programme even if events can still fire.(Easy).
  5. 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

  1. 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).
  2. Re-do 1) above using AIE objects and the interruptible interface. (Easy).
  3. 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).
  4. Implement and test the Persistent AIE given in section 13.5 of the draft book (More difficult).
  5. Devise a program to illustrate the semantics of multiple AIEs (section 13.6 of the draft book) (Difficult).
  6. 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)
  7. Test the impreciase computation examle given in sectio 13.7 of the draft book. (More difficult).
  8. Does the Reference Implementation support the POSIX Sign al interface? Try and write a program that catches control-C. (Difficult).
Читайте также:  Морфа нормал королевский питон

Resource Sharing

Example Examination Questions

Example Question on Java Thread Model

  1. Summarize the strengths and weaknesses of the Java Concurrency model
  2. Consider the following class

// 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

  1. Explain the approach to memory management in the RTSJ (13 marks).
  2. 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)

The parent creates a new schedulable object with an initial memory area of scopedE. Draw the new schedulable objects scoped memory stack. (3 marks)

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)

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

  1. Explain briefly the RTSJ's models of asynchronous transfer of control and asynchronous event handler (12 marks)
  2. 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();
>
>

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.

Источник

Оцените статью