© Andy Wellings, 2004 Roadmap  Introduction  Concurrent Programming  Communication and Synchronization  Completing the Java Model  Overview of the.

Slides:



Advertisements
Similar presentations
50.003: Elements of Software Construction Week 6 Thread Safety and Synchronization.
Advertisements

© Andy Wellings, 2004 Roadmap  Introduction  Concurrent Programming  Communication and Synchronization  Completing the Java Model  Overview of the.
Ade Azurat, Advanced Programming 2004 (Based on LYS Stefanus’s slides) Advanced Programming 2004, Based on LYS Stefanus’s slides Slide 2.1 Multithreading.
Real-Time Systems and Programming Languages © Alan Burns and Andy Wellings Chapter 9: Real-Time Facilities.
© Andy Wellings, 2004 Roadmap  Introduction  Concurrent Programming  Communication and Synchronization  Completing the Java Model  Overview of the.
© Andy Wellings, 2004 Roadmap  Introduction  Concurrent Programming  Communication and Synchronization  synchronized methods and statements  wait.
Instructore: Tasneem Darwish1 University of Palestine Faculty of Applied Engineering and Urban Planning Software Engineering Department Concurrent and.
Inheritance Inheritance Reserved word protected Reserved word super
Unit 141 Threads What is a Thread? Multithreading Creating Threads – Subclassing java.lang.Thread Example 1 Creating Threads – Implementing java.lang.Runnable.
Algorithm Programming Concurrent Programming in Java Bar-Ilan University תשס"ח Moshe Fresko.
Programming R-T Abstractions TSW November 2009 Anders P. Ravn Aalborg University.
© Andy Wellings, 2004 Roadmap  Introduction  Concurrent Programming  Communication and Synchronization  Completing the Java Model  Overview of the.
Real-Time Systems Specification and Analysis ITV Mutiprogramming and Real-Time Programs Anders P. Ravn Aalborg University May 2009.
Real-Time Java Real-Time Systems Anders P. Ravn Aalborg University March 2008.
© Andy Wellings, 2004 Roadmap  Introduction  Concurrent Programming  Communication and Synchronization  Completing the Java Model  Overview of the.
© Andy Wellings, 2004 Roadmap  Introduction  Concurrent Programming  Communication and Synchronization  Completing the Java Model  Overview of the.
© Andy Wellings, 2004 Roadmap  Introduction  Concurrent Programming  Communication and Synchronization  Completing the Java Model  Overview of the.
© Andy Wellings, 2004 Roadmap  Introduction  Concurrent Programming  Communication and Synchronization  Completing the Java Model  Overview of the.
Real-Time Systems Specification and Analysis ITV Real-Time Systems Anders P. Ravn Aalborg University February 2006.
© Andy Wellings, 2004 Roadmap  Introduction  Concurrent Programming  Communication and Synchronization  Completing the Java Model  Overview of the.
© Andy Wellings, 2003 Roadmap  Introduction  Concurrent Programming  Communication and Synchronization  Completing the Java Model  Overview of the.
Synchronization in Java Fawzi Emad Chau-Wen Tseng Department of Computer Science University of Maryland, College Park.
© Andy Wellings, 2004 Roadmap  Introduction  Concurrent Programming  Communication and Synchronization  Completing the Java Model  Overview of the.
Real-Time Systems Specification and Analysis ITV Real-Time Systems Anders P. Ravn Aalborg University February 2009.
The Real-Time Java Profile ITV Real-Time Systems Anders P. Ravn Aalborg University February 2006.
© Andy Wellings, 2004 Roadmap  Introduction  Concurrent Programming  Communication and Synchronization  Completing the Java Model  Overview of the.
© Andy Wellings, 2004 Roadmap  Introduction  Concurrent Programming  Communication and Synchronization  Completing the Java Model  Overview of the.
Chair of Software Engineering ATOT - Lecture 21, 16 June 2003 Advanced Topics in Object Technology Bertrand Meyer.
© Andy Wellings, 2004 Roadmap  Introduction  Concurrent Programming  Communication and Synchronization  Completing the Java Model  Overview of the.
Parallel Processing (CS526) Spring 2012(Week 8).  Thread Status.  Synchronization in Shared Memory Programming(Java threads ) ◦ Locks ◦ Barriars.
A Safety-Critical Java Technology Compatibility Kit Hans Søndergaard Stephan Korsholm VIA University College, Horsens, Denmark & Anders P. Ravn Aalborg.
Chapter 15 Multithreading F Threads Concept  Creating Threads by Extending the Thread class  Creating Threads by Implementing the Runnable Interface.
Java Programming: Advanced Topics
Object Oriented Programming Lecture 8: Introduction to laboratorial exercise – part II, Introduction to GUI frames in Netbeans, Introduction to threads.
Threads. Overview Problem Multiple tasks for computer Draw & display images on screen Check keyboard & mouse input Send & receive data on network Read.
Threading and Concurrency Issues ● Creating Threads ● In Java ● Subclassing Thread ● Implementing Runnable ● Synchronization ● Immutable ● Synchronized.
Quick overview of threads in Java Babak Esfandiari (extracted from Qusay Mahmoud’s slides)
Instructore: Tasneem Darwish1 University of Palestine Faculty of Applied Engineering and Urban Planning Software Engineering Department Concurrent and.
111 © 2002, Cisco Systems, Inc. All rights reserved.
Semaphores, Locks and Monitors By Samah Ibrahim And Dena Missak.
CSC321 Concurrent Programming: §5 Monitors 1 Section 5 Monitors.
Threads.
11 G53SRP: Clocks and Times in RTSJ Chris Greenhalgh School of Computer Science.
EEL The Real-Time Specification for Java (1)
Multithreading in Java Sameer Singh Chauhan Lecturer, I. T. Dept., SVIT, Vasad.
Introduction to Threads Session 01 Java Simplified / Session 14 / 2 of 28 Objectives Define a thread Define multithreading List benefits of multithreading.
What is a ‘ thread ’ ? Free Online Dictionary of Computing (FOLDOC) Sharing a single CPU between multiple tasks (or "threads") in a way designed to minimize.
Multithreading in JAVA
1 G53SRP: Clocks and Time in Java Chris Greenhalgh School of Computer Science.
Java Thread and Memory Model
Java Producer-Consumer Monitor From: Concurrent Programming: The Java Programming Language By Steven J. Hartley Oxford University Press, 1998.
Threads in Java Threads Introduction: After completing this chapter, you will be able to code your own thread, control them efficiently without.
Thread A thread represents an independent module of an application that can be concurrently execution With other modules of the application. MULTITHREADING.
CSI 3125, Preliminaries, page 1 Inheritance. CSI 3125, Preliminaries, page 2 Inheritance Using inheritance, can create a general class that defines traits.
Threads b A thread is a flow of control in a program. b The Java Virtual Machine allows an application to have multiple threads of execution running concurrently.
Chapter 13: Multithreading The Thread class The Thread class The Runnable Interface The Runnable Interface Thread States Thread States Thread Priority.
© Andy Wellings, 2004 Thread Priorities I  Although priorities can be given to Java threads, they are only used as a guide to the underlying scheduler.
1 Java Programming Java Programming II Concurrent Programming: Threads ( II)
Concurrency in Java MD. ANISUR RAHMAN. slide 2 Concurrency  Multiprogramming  Single processor runs several programs at the same time  Each program.
Adding Concurrency to a Programming Language Peter A. Buhr and Glen Ditchfield USENIX C++ Technical Conference, Portland, Oregon, U. S. A., August 1992.
Chapter 13: Multithreading
G53SRP: Real Time Threads in RTSJ (part I)
Threads Chate Patanothai.
Multithreading.
Multithreaded Programming
Chapter 15 Multithreading
NETWORK PROGRAMMING CNET 441
CS 112 Programming 2 Lecture 02 Abstract Classes & Interfaces (2)
Concurrent programming
Java Chapter 3 (Estifanos Tilahun Mihret--Tech with Estif)
Presentation transcript:

© Andy Wellings, 2004 Roadmap  Introduction  Concurrent Programming  Communication and Synchronization  Completing the Java Model  Overview of the RTSJ  Memory Management  Clocks and Time  Scheduling and Schedulable Objects  Asynchronous Events and Handlers  Real-Time Threads  Asynchronous Transfer of Control  Resource Control  Schedulability Analysis  Conclusions

© Andy Wellings, 2004 Clocks and Time Lecture aims  To provide some background on clocks and time  To explain absolute and relative time values  To consider the support for real-time clocks  To give some simple examples

© Andy Wellings, 2004 Introduction I  Standard Java only supports the notion of a wall clock (calendar time); for many applications, a clock based on UTC (Coordinated Universal Time) is sufficient  However real-time systems often require  A monotonic clock which progresses at a constant rate and is not subject to the insertion of extra ticks to reflect leap seconds (as UTC clocks are). A constant rate is needed for control algorithms which want to executed on a regular basis. Many monotonic clocks are also relative to system startup and can be used only to measure the passage of time, not calendar time  A count down clock which can be paused, continued or reset (for example the clock which counts down to the launch of the Space Shuttle)  A CPU execution time clock which measures the amount of CPU time that is being consumed by a particular thread or object

© Andy Wellings, 2004 Introduction II  All of the above clocks also need a resolution which is potentially finer than the millisecond level  They may all be based on the same underlying physical clock, or be supported by separate clocks  Where more than one clock is provided, issues of the relationship between them may be importance; in particular, whether their values can drift apart  We consider the additional clock and time classes that are provided by the RTSJ to augment the standard Java facilities

© Andy Wellings, 2004 Basic Model  The RTSJ defines a hierarchy of time classes rooted on the abstract HighResolutionTime class  This abstract class has three subclasses:  one which represents absolute time  one which represents relative time  and one which represents rational time  The intention is to allow support for time values down to the nanosecond accuracy  Clocks are supported through an abstract Clock class

© Andy Wellings, 2004 High Resolution Time I public abstract class HighResolutionTime implements Comparable, Cloneable { // methods public abstract AbsoluteTime absolute(Clock clock); public int compareTo(HighResolutionTime time); public boolean equals(HighResolutionTime time); public final long getMilliseconds(); public final int getNanoseconds(); public abstract RelativeTime relative(Clock clock); public void set(HighResolutionTime time); public void set(long millis, int nanos); public static void waitForObject(Object target, HighResolutionTime time) throws InterruptedException;... }

© Andy Wellings, 2004 High Resolution Time II  The abstract methods ( absolute and relative ) allow time types that are relative to be re-expressed as absolute time values and vice versa.  The methods also allow the clocks associated with the values to be changed.  absolute to absolute  The value returned has the same millisecond and nanosecond components as the encapsulated time value  absolute to relative  The value returned is the value of the encapsulated absolute time minus the current time as measured from the given clock parameter  relative to relative  The value returned has the same millisecond and nanosecond components as the encapsulated time value  relative to absolute  The value returned is the value of current time as measured from the given clock parameter plus the encapsulated relative time

© Andy Wellings, 2004 High Resolution Time III  Changing the clock associated with a time value is potentially unsafe, particularly for absolute time values  This is because absolute time values are represented as a number of milliseconds and nanoseconds since an epoch  Different clocks may have different epochs.  The waitForObject does not resolve the problem of determining if the schedulable object was woken by a notify method call or by a timeout  It does allow both relative and absolute time values to be specified.

© Andy Wellings, 2004 Absolute Time I public class AbsoluteTime extends HighResolutionTime { // constructors public AbsoluteTime(); public AbsoluteTime(AbsoluteTime time); public AbsoluteTime(java.util.Date date); public AbsoluteTime(long millis, int nanos); // methods... } Note that an absolute time can have either a positive or a negative value and that, by default, it is relative to the epoch of the real-time clock

© Andy Wellings, 2004 Absolute Time II public class AbsoluteTime extends HighResolutionTime {... public AbsoluteTime absolute(Clock clock); public AbsoluteTime add(long millis, int nanos); public final AbsoluteTime add(RelativeTime time); public java.util.Date getDate(); public RelativeTime relative(Clock clock); public void set(java.util.Date date); public RelativeTime subtract(AbsoluteTime time); public AbsoluteTime subtract(RelativeTime time); public String toString(); }

© Andy Wellings, 2004 Relative Time I public class RelativeTime extends HighResolutionTime { // constructors public RelativeTime(); public RelativeTime(long millis, int nanos); public RelativeTime(RelativeTime time);... } A relative time value is an interval of time measured by some clock; for example 20 milliseconds

© Andy Wellings, 2004 Relative Time II public class RelativeTime extends HighResolutionTime {... // methods public AbsoluteTime absolute(Clock clock); public RelativeTime add(long millis, int nanos); public RelativeTime add(RelativeTime time); public RelativeTime relative(Clock clock); public RelativeTime subtract(RelativeTime time); public String toString();... }

© Andy Wellings, 2004 Clocks  The RTSJ Clock class defines the abstract class from which all clocks are derived  The language allows many different types of clocks; eg, an execution-time clock which measures the amount of execution time being consumed  There is real-time clock which advances monotonically  can never go backwards  should progress uniformly and not be subject to the insertion of leap ticks  The static method getRealtimeClock allows this clock to be obtained  Other methods are provided to get the resolution of a clock and, if the hardware permits, to set the resolution of a clock

© Andy Wellings, 2004 The Clock Class public abstract class Clock { // constructor public Clock(); // methods public static Clock getRealtimeClock(); public abstract RelativeTime getResolution(); public AbsoluteTime getTime(); public abstract void getTime(AbsoluteTime time); public abstract void setResolution( RelativeTime resolution); }

© Andy Wellings, 2004 Example: measuring elapse time { AbsoluteTime oldTime, newTime; RelativeTime interval; Clock clock = Clock.getRealtimeClock(); oldTime = clock.getTime(); // other computations newTime = clock.getTime(); interval = newTime.subtract(oldTime); } This approach would only measure the approximate elapse time, as the schedulable object executing the code may be pre- empted after it has finished the computation and before it reads the new time

© Andy Wellings, 2004 Example: A launch clock  A launch clock is clock which is initiated with a relative time value and an absolute time value  The absolute time value is the time at which the clock is to start ticking; the relative time value is the duration of the countdown  The count down can be stopped, restarted, or reset  The class extends the Thread class  The constructor saves the start time, the duration and the clock to be used  The resolution of the count down is one second, and various functions allow the current launch time to be queried

© Andy Wellings, 2004 LaunchClock I public class LaunchClock extends Thread { public LaunchClock(AbsoluteTime at, RelativeTime countDown) { super(); startTime = at; remainingTime = countDown; myClock = Clock.getRealtimeClock(); counting = true; go = false; tick = new RelativeTime(1000,0); } private AbsoluteTime startTime; private RelativeTime remainingTime; private Clock myClock; private boolean counting; private boolean go; private RelativeTime tick;

© Andy Wellings, 2004 LaunchClock II public RelativeTime getResolution() { return tick; } public synchronized AbsoluteTime getCurrentLaunchTime() { return new AbsoluteTime( myClock.getTime().add(remainingTime)); // assumes started and ticking }

© Andy Wellings, 2004 LaunchClock III public synchronized void stopCountDown() { counting = false; notifyAll(); } public synchronized void restartCountDown() { counting = true; notifyAll(); } public synchronized void resetCountDown( RelativeTime to) { remainingTime = to; }

© Andy Wellings, 2004 LaunchClock IV public synchronized void launch() throws Exception { while(!go){ try { wait(); } catch(InterruptedException ie) { throw new Exception("Launch failed"); } // Launch is go }

© Andy Wellings, 2004 LaunchClock V public void run() { try { synchronized(this) { while(myClock.getTime().compareTo(startTime) < 0) HighResolutionTime. waitForObject(this, startTime); while(remainingTime.getMilliseconds() > 0) { while(!counting) wait(); HighResolutionTime.waitForObject(this, tick); remainingTime.set( remainingTime.getMilliseconds() - tick.getMilliseconds()); } go = true; notifyAll(); } catch(InterruptedException ie) { } }

© Andy Wellings, 2004 Summary  Clocks and time are fundamental to any real-time system  The RTSJ has augmented the standard Java facilities with high resolution time types and a framework for supporting various clocks  A real-time clock is guaranteed to be supported by any compliant RTSJ implementation  This is a monotonically non-decreasing clock

© Andy Wellings, 2004 Further Reading and Exercises