Real-Time Java * Programming Christopher D. Gill Center for Distributed Object Computing Department of Computer Science Washington.

Slides:



Advertisements
Similar presentations
Practice Session 7 Synchronization Liveness Deadlock Starvation Livelock Guarded Methods Model Thread Timing Busy Wait Sleep and Check Wait and Notify.
Advertisements

Concurrency Important and difficult (Ada slides copied from Ed Schonberg)
© Andy Wellings, 2004 Roadmap  Introduction  Concurrent Programming  Communication and Synchronization  Completing the Java Model  Overview of the.
CS 5704 Fall 00 1 Monitors in Java Model and Examples.
1 CSC321 §2 Concurrent Programming Abstraction & Java Threads Section 2 Concurrent Programming Abstraction & Java Threads.
Multithreading The objectives of this chapter are:
© Andy Wellings, 2004 Roadmap  Introduction  Concurrent Programming  Communication and Synchronization  Completing the Java Model  Overview of the.
Scheduling in Batch Systems
© 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.
1 Threads (Part I) Introduction to Threads and Concurrency Overview  Introduction to Threads of Computation  Creating Threads in java an Example  Thread.
© 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.
Fundamentals of Python: From First Programs Through Data Structures
Real-Time Software Design Yonsei University 2 nd Semester, 2014 Sanghyun Park.
OPERATING SYSTEMS CPU SCHEDULING.  Introduction to CPU scheduling Introduction to CPU scheduling  Dispatcher Dispatcher  Terms used in CPU scheduling.
Chapter 4: Threads. 4.2CSCI 380 Operating Systems Chapter 4: Threads Overview Multithreading Models Threading Issues Pthreads Windows XP Threads Linux.
Real-Time Java Martin Schöberl. Real Time Java2 Overview What are real-time systems Real-time specification for Java RTSJ issues, subset Real-time profile.
Object Oriented Analysis & Design SDL Threads. Contents 2  Processes  Thread Concepts  Creating threads  Critical sections  Synchronizing threads.
CMSC 202 Exceptions. Aug 7, Error Handling In the ideal world, all errors would occur when your code is compiled. That won’t happen. Errors which.
Threading and Concurrency Issues ● Creating Threads ● In Java ● Subclassing Thread ● Implementing Runnable ● Synchronization ● Immutable ● Synchronized.
Operating Systems Lecture 2 Processes and Threads Adapted from Operating Systems Lecture Notes, Copyright 1997 Martin C. Rinard. Zhiqing Liu School of.
Quick overview of threads in Java Babak Esfandiari (extracted from Qusay Mahmoud’s slides)
Distributed Real- Time in the RTSJ Andrew Borg. Presentation RMI – A brief introduction The RTSJ – A (very) brief introduction The DRTSJ – The 3 Levels.
Today’s Agenda  Quick Review  Finish Java Threads  The CS Problem Advanced Topics in Software Engineering 1.
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.
Enhancements to Java for Real Time Systems Theresa Dsena CSE Fall 2006 Prof. Ganesan.
Java Threads. What is a Thread? A thread can be loosely defined as a separate stream of execution that takes place simultaneously with and independently.
1 (Worker Queues) cs What is a Thread Pool? A collection of threads that are created once (e.g. when a server starts) That is, no need to create.
Internet Software Development Controlling Threads Paul J Krause.
Prepare by : Ihab shahtout.  Overview  To give an overview of fixed priority schedule  Scheduling and Fixed Priority Scheduling.
CSC321 Concurrent Programming: §5 Monitors 1 Section 5 Monitors.
Threaded Programming in Python Adapted from Fundamentals of Python: From First Programs Through Data Structures CPE 401 / 601 Computer Network Systems.
EEL The Real-Time Specification for Java (1)
Concurrency in Java Brad Vander Zanden. Processes and Threads Process: A self-contained execution environment Thread: Exists within a process and shares.
EEL Real-time Java part 2. EEL Acknowledgements All the lecture slides were adopted from the slides of Andy Wellings.
Java Thread and Memory Model
CE Operating Systems Lecture 2 Low level hardware support for operating systems.
SPL/2010 Guarded Methods and Waiting 1. SPL/2010 Reminder! ● Concurrency problem: asynchronous modifications to object states lead to failure of thread.
Threads in Java1 Concurrency Synchronizing threads, thread pools, etc.
CE Operating Systems Lecture 2 Low level hardware support for operating systems.
Thread basics. A computer process Every time a program is executed a process is created It is managed via a data structure that keeps all things memory.
Threads in Java Threads Introduction: After completing this chapter, you will be able to code your own thread, control them efficiently without.
Time Management.  Time management is concerned with OS facilities and services which measure real time.  These services include:  Keeping track of.
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.
1.  System Characteristics  Features of Real-Time Systems  Implementing Real-Time Operating Systems  Real-Time CPU Scheduling  An Example: VxWorks5.x.
Concurrency (Threads) Threads allow you to do tasks in parallel. In an unthreaded program, you code is executed procedurally from start to finish. In a.
Concurrency in Java MD. ANISUR RAHMAN. slide 2 Concurrency  Multiprogramming  Single processor runs several programs at the same time  Each program.
Concurrent Programming in Java Based on Notes by J. Johns (based on Java in a Nutshell, Learning Java) Also Java Tutorial, Concurrent Programming in Java.
Multithreading The objectives of this chapter are:
A brief intro to: Parallelism, Threads, and Concurrency
REAL-TIME OPERATING SYSTEMS
Multithreading / Concurrency
Processes and threads.
Thread Pools (Worker Queues) cs
Process concept.
Thread Pools (Worker Queues) cs
Background on the need for Synchronization
Advanced Topics in Concurrency and Reactive Programming: Asynchronous Programming Majeed Kassis.
Chapter 5a: CPU Scheduling
Compiler Design 18. Object Oriented Semantic Analysis (Symbol Tables, Type Checking) Kanat Bolazar March 30, 2010.
Half-Sync/Half-Async (HSHA) and Leader/Followers (LF) Patterns
Multithreaded Programming
Real Time Java : Synchronization
Threads and Multithreading
CMSC 202 Exceptions.
Multithreading The objectives of this chapter are:
Java Chapter 3 (Estifanos Tilahun Mihret--Tech with Estif)
Presentation transcript:

Real-Time Java * Programming Christopher D. Gill Center for Distributed Object Computing Department of Computer Science Washington University, St. Louis COOTS 2001 Tutorial M4 Monday, January 29, 2001 * Java TM is a registered trademark of Sun Microsystems

Christopher D. Gill Tutorial Objectives Provide an overview of real-time programming issues Describe a motivating real-time programming example –An on-line stock market analysis tool Exhibits canonical requirements and issues common to other classes of real-time systems Show through incremental evolution of the example –How real-time programming issues can arise in a Java TM (Java) programming environment –How features of the Real-Time Specification for Java TM (RTSJ) can be applied to resolve these issues Real-Time Java Programming

Christopher D. Gill Example: Stock Market Analysis Tool Performs automated decision aiding for stock trading Inputs arrive from real-time data streams May run queries against on-line databases Sends alerts to human operator and/or other automated systems with specific recommendations (e.g., sell, buy, limit order, short, call, put) Timeliness of outputs is crucial –A functionally correct output sent too late can be worse than no output at all Several application layers compete in real-time for system resources (i.e., CPU, memory) Real-Time Java Programming

Christopher D. Gill Example: Stock Market Analysis Tool Inputs arrive in real-time from data streams –Real-time (seconds) arrival of data events –One feed per market May run queries on-line tables and databases: differences in latency and latency jitter –Analyst reports –Market histories –Sector P/E tables DataFeed NYSEFeedNasdaqFeed DataStore NasdaqStore ResearchStore NYSEStore Real-Time Java Programming

Christopher D. Gill Example: Stock Market Analysis Tool Sends recommendations as alerts to: –Human operators –Automated systems Documented quality of information is key –Decision path, triggers –Additional info, links Timeliness constraints must also be met –Incremental addition, refinement is useful MarketOrder Option Call Annotation Alert PutBuySell * Real-Time Java Programming

Christopher D. Gill Example: Stock Market Analysis Tool Input events pass through an analysis pipeline –Each analysis filter handles the data and news events in which it is interested, may search databases –May attach additional information to event and pass it on or consume it, and/or produce alerts –Composites combine other analysis filters AnalysisPipelineAnalysisFilter SectorPEPortfolioBalance 1+1+ Composite 1+1+ Real-Time Java Programming

Christopher D. Gill Example: Roadmap NasdaqAnnotation AnalysisPipeline AlertList SectorPEFilterPortfolioBalanceFilterCompositeFilter MarketOrderAlertOptionAlert CallAlert Alert PutAlert BuyAlertSellAlert AnalysisFilter AnalysisTool Annotation ResearchAnnotation DataStore NasdaqStoreResearchStore AnnotationList Portfolio DataFeed DataFeedEvent NasdaqDataFeed Real-Time Java Programming

Christopher D. Gill Example: Stock Market Analysis Tool // Input Event Streams Code public class DataFeedEvent { private float bid; private float ask; private float change; private long volume; //... public DataFeedEvent (float b, float a, float c, long v) {bid = b; ask = a; change = c; volume = v;} public float getBid () {return bid;} public float getAsk () {return ask;} public float getChange () {return change;} public long getVolume () {return volume;} //... } Real-Time Java Programming data event 90 seconds data feed Market market order

Christopher D. Gill Example: Stock Market Analysis Tool // Input Event Streams Code, Continued public abstract class DataFeed { public abstract DataFeedEvent pullDataFeedEvent (); } public class NasdaqDataFeed extends DataFeed { // low-ish latency public DataFeedEvent pullDataFeedEvent () { return pullNasdaqDataFeedEvent (); } protected DataFeedEvent pullNasdaqDataFeedEvent () { float bid = 0.0F; float ask = 0.0F; float chg = 0.0F; long vol = 0; // read data from socket, etc... return new DataFeedEvent (bid, ask, chg, vol); } } /*... Other DataFeed Classes... */ Real-Time Java Programming Separate data feed for each market Low latency to pull an event from a market data feed

Christopher D. Gill Example: Roadmap AnalysisPipeline AlertList SectorPEFilterPortfolioBalanceFilterCompositeFilter AnalysisFilter AnalysisTool DataStore NasdaqStoreResearchStore Portfolio DataFeed DataFeedEvent NasdaqDataFeed NasdaqAnnotation MarketOrderAlertOptionAlert CallAlert Alert PutAlert BuyAlertSellAlert Annotation ResearchAnnotation AnnotationList Real-Time Java Programming

Christopher D. Gill Example: Stock Market Analysis Tool // Alerts Code public abstract class Annotation { /*... */ } public class AnnotationList { private java.util.Vector alist; // list of annotations public void addSorted (Annotation a) { /*... */ } } public abstract class Alert { private AnnotationList anotes; private DataFeedEvent trigger; Alert (DataFeedEvent dfe) {anotes = new AnnotationList (); trigger = dfe;} public DataFeedEvent getTrigger () {return trigger;} public void addAnnotation (Annotation a) { anotes.addSorted (a); } public Annotation nextAnnotation (boolean restart) { /* move to next annotation in list, return it... */ } } Real-Time Java Programming trigger Alert annotations

Christopher D. Gill Example: Stock Market Analysis Tool // Alerts Code, Continued public abstract class MarketOrderAlert extends Alert { private float orderPrice; private String symbol; public MarketOrderAlert (DataFeedEvent dfe, float op, String s) {super (dfe); orderPrice = op; symbol = s;} protected String getSymbol () {return symbol;} protected float getOrderPrice () {return orderPrice;} } /*... Similarly, for OptionAlert and its derived classes... */ public class BuyAlert extends MarketOrderAlert { public BuyAlert (DataFeedEvent dfe, float op, String s) {super (dfe, op, s);} float getBuyPrice () { return super.getOrderPrice (); } } /*... Similarly for SellAlert, Other Alert Classes... */ Real-Time Java Programming

Christopher D. Gill Example: Stock Market Analysis Tool // Data Store Query Code public class NasdaqAnnotation extends Annotation { private float sectorAvgEarnings; private float sectorPERatio; public NasdaqAnnotation (float e, float r) {sectorAvgEarnings = e; sectorPERatio = r;} public float getSectorAvgEarnings () {return sectorAvgEarnings;} public float getSectorPERatio () {return sectorPERatio;} }/*... Other Annotation Classes */ public class ResearchAnnotation extends Annotation { // URLs for research reports private java.util.Vector research_reports; public void addReport (java.net.URL u) {reports.add (u);} public java.net.URL nextReport (boolean restart) { /*... */ } }/*... Other Annotation Classes */ Real-Time Java Programming annotations research reports URL sector analysis table P/E

Christopher D. Gill Example: Roadmap AnalysisPipeline AlertList SectorPEFilterPortfolioBalanceFilterCompositeFilter AnalysisFilter AnalysisTool Portfolio DataFeed DataFeedEvent NasdaqDataFeed NasdaqAnnotation MarketOrderAlertOptionAlert CallAlert Alert PutAlert BuyAlertSellAlert Annotation ResearchAnnotation AnnotationList DataStore NasdaqStoreResearchStore Real-Time Java Programming

Christopher D. Gill Example: Stock Market Analysis Tool // Data Store Query Code, Continued public abstract class DataStore { public abstract void annotateAlert (Alert a);} public class NasdaqStore extends DataStore { public float getPE (String symbol, boolean sector) {/* medium duration */} public float getEarnings (String symbol) {/*...*/} public void annotateAlert (Alert a) { addNasdaqAnnotation (a); /*... */ } protected void addNasdaqAnnotation (Alert a) { float e = 0.0F; float r = 0.0F; // compute PE and Earnings averages for the sector a.addAnnotation (new NasdaqAnnotation (e, r)); } Real-Time Java Programming annotations sector analysis table P/E Nasdaq market history database analysis query

Christopher D. Gill Example: Stock Market Analysis Tool // Data Store Query Code, Continued public class ResearchStore extends DataStore { public void annotateAlert (Alert a) { addResearchAnnotation (a);} protected void addResearchAnnotation (Alert a) { // long duration: guided // search for research // reports, adding URLS // for relevant analyst // research reports to // the annotation // (ordered by relevance // & confidence factors) // add annotation to alert a.addAnnotation (new ResearchAnnotation ()); } } /*... Other DataStore Classes... */ Real-Time Java Programming annotations hyper- linked research reports URL report index search agent

Christopher D. Gill Example: Roadmap DataFeed DataFeedEvent NasdaqDataFeed NasdaqAnnotation MarketOrderAlertOptionAlert CallAlert Alert PutAlert BuyAlertSellAlert Annotation ResearchAnnotation AnnotationList DataStore NasdaqStoreResearchStore AnalysisPipeline AlertList SectorPEFilterPortfolioBalanceFilterCompositeFilter AnalysisFilter AnalysisTool Portfolio Real-Time Java Programming

Christopher D. Gill Example: Stock Market Analysis Tool // Analysis Filter Code public class AlertList {// Alerts raised so far private java.util.Vector alerts; public void addAlert (Alert a) {alerts.add (a);} public Alert nextReport (boolean restart) { /*... */ } public void reset () { alerts.clear ();} } public abstract class AnalysisFilter {public abstract boolean handleDataEvent (DataFeedEvent d, AlertList a); //... } Real-Time Java Programming data event data feed Analysis filter alert list

Christopher D. Gill Example: Stock Market Analysis Tool // Analysis Filter Code, Continued public class CompositeFilter extends AnalysisFilter { // the composed filters private java.util.Vector filters; public void addFilter (AnalysisFilter af) { filters.add (af); } public boolean handleDataEvent (DataFeedEvent dfe, AlertList al) { boolean consumed = false; for (int i = 0; !consumed && i < filters.size (); ++i) { consumed = ((AnalysisFilter) filters.get(i)).handleDataEvent (dfe, al); } return consumed; } Real-Time Java Programming data event Composite Filter

Christopher D. Gill Example: Stock Market Analysis Tool // Analysis Filter Code, Continued public class SectorPEFilter extends AnalysisFilter { private NasdaqStore nh; private ResearchStore rr; public boolean handleDataEvent (DataFeedEvent dfe, AlertList al) { boolean consumed = false; // See if event is of interest, // compare its PE to the avg for // its sector, look at existing // alerts, possibly generate // new ones annotated with // relevant research reports rr.annotateAlert (alert) return consumed; } Real-Time Java Programming research reports sector analysis table Sector P/E Filter data event

Christopher D. Gill Example: Stock Market Analysis Tool // Analysis Filter Code, Continued public class Portfolio { public float projectRiskDelta (DataFeedEvent d) {/*...*/} public float projectGainDelta (DataFeedEvent d) {/*...*/} } public class PortfolioBalanceFilter extends AnalysisFilter { protected Portfolio p; public boolean handleDataEvent (DataFeedEvent dfe, AlertList al) { boolean consumed = false; // issue/remove alerts based on // data feed event and projected // risk/gain to portfolio goals return consumed; } Real-Time Java Programming Portfolio Balance Filter alert list goals risk profile data event

Christopher D. Gill Example: Stock Market Analysis Tool // Analysis Pipeline Code public class AnalysisPipeline { private CompositeFilter cf; private DataFeed df; private AlertList al; public void addFilter (AnalysisFilter af) {cf.addFilter (af);} public void sendAlerts () {/* Send all alerts, reset list */} public void run () { for (;;) { DataFeedEvent dfe = df.pullDataFeedEvent (); cf.handleDataEvent (dfe, al); // possibly long latency sendAlerts (); /* latency depends on alert count */} } Real-Time Java Programming data event Filter Pipeline alert list send alerts Operator

Christopher D. Gill Example: Stock Market Analysis Tool // Analysis Tool Code public class AnalysisTool { public static void main (String [] args) { AnalysisPipeline ap = new AnalysisPipeline (); ap.addFilter (new PortfolioBalanceFilter ()); ap.addFilter (new SectorPEFilter ()); ap.run (); // run the pipeline } Real-Time Java Programming data event data feed Market market order alert list send alerts

Christopher D. Gill Review: Roadmap AnalysisPipeline AlertList SectorPEFilterPortfolioBalanceFilterCompositeFilter MarketOrderAlertOptionAlert CallAlert DataFeedAlert PutAlert BuyAlertSellAlert AnalysisFilter AnalysisTool AnnotationDataFeedEvent NasdaqDataFeed NasdaqAnnotation ResearchAnnotation DataStore NasdaqStoreResearchStore AnnotationList Portfolio Real-Time Java Programming

Christopher D. Gill Example: Time Scales AlertList MarketOrderAlertOptionAlert CallAlert Alert PutAlert BuyAlertSellAlert AnalysisTool DataFeedEvent ResearchAnnotation ResearchStore AnnotationList Portfolio Low MediumLatency:High AnalysisPipeline SectorPEFilterPortfolioBalanceFilterCompositeFilter DataFeed AnalysisFilter NasdaqDataFeed NasdaqAnnotation DataStore NasdaqStore Annotation AnalysisTool ResearchAnnotation ResearchStore Real-Time Java Programming

Christopher D. Gill Java Real-Time Issues Existing Java TM facilities take us several important steps in the direction of real-time application behavior Threads –Liveness (what and how much happens) –Threads are used to decouple activity time scales Synchronization –Safety (nothing “unsafe” happens) –Careful application of monitors can preserve liveness We’ll start in a bottom-up liveness-first design mode, using thread adapters (Lea, “Concurrent Programming in Java TM ”) Real-Time Java Programming

Christopher D. Gill Separate threads of execution are useful to improve liveness by doing the following concurrently: –Getting and handling market data events Medium latency –Searching stores to add annotations High latency –Issuing alerts Low latency // Analysis Tool Code, Revisited // Separate high latency activity public class StoreThreadAdapter implements Runnable { private DataStore store; private Alert alert; public StoreThreadAdapter (DataStore ds, Alert a) { store = ds; alert = a;} public void run () { store.annotateAlert (alert); } Java: Threading Issues Real-Time Java Programming

Christopher D. Gill Java: Threading Issues // Analysis Filter Code, Revisited public class SectorPEFilter extends AnalysisFilter { private NasdaqStore nh; private ResearchStore rr; public boolean handleDataEvent (DataFeedEvent dfe, AlertList al) { boolean consumed = false; // possibly generate new alerts... //... annotated with relevant research reports... Thread annotationThread = new Thread (new StoreThreadAdapter (rr, alert)); annotationThread.setPriority (Thread.MIN_PRIORITY); annotationThread.start (); return consumed; } Real-Time Java Programming

Christopher D. Gill Java: Threading Issues // Analysis Tool Code, Revisited // Separate low latency activity public class AlertThreadAdapter implements Runnable { private AnalysisPipeline pipeline; private long timeout; public AlertThreadAdapter (AnalysisPipeline ap, long t) { pipeline = ap; timeout = t;} public void run () { for (;;) // in reality, could use more sophisticated { // loop control e.g., wait, notifyAll, etc. try { Thread.sleep (timeout); pipeline.sendAlerts (); } catch (java.lang.InterruptedException e) {/*... */} } Real-Time Java Programming

Christopher D. Gill Java: Threading Issues // Analysis Pipeline Code, Revisited // Separate medium latency activity public class AnalysisPipeline { private CompositeFilter cf; // filters in the pipeline private DataFeed df; // paced data event feed private AlertList al; // list of alerts public void addFilter (AnalysisFilter af) {cf.addFilter (af);} public void sendAlerts () {/* Send all alerts in the list, reset alert list */} public void run () { for (;;) { DataFeedEvent dfe = df.pullDataFeedEvent (); cf.handleDataEvent (dfe, al); // possibly long latency } Real-Time Java Programming

Christopher D. Gill Java: Threading Issues // Analysis Tool Code, Revisited public class AnalysisTool { public static void main (String [] args) { AnalysisPipeline ap = new AnalysisPipeline (); ap.addFilter (new PortfolioBalanceFilter ()); ap.addFilter (new SectorPEFilter ()); Thread alertThread = new Thread (new AlertThreadAdapter (ap, 1000)); alertThread.setPriority (Thread.MAX_PRIORITY); alertThread.start (); ap.run (); // run pipeline in the current thread } Real-Time Java Programming

Christopher D. Gill Java: Synchronization Issues But, before we go further addressing liveness issues, need to address concurrency safety Shift to top-down safety- first design mode, using fine-grain synchronization (Lea, “Concurrent Programming in Java TM ”) We’ll combine two styles: block and method synchronization // Concurrency safety additions // using method synchronization public abstract class Alert { /*... */ public synchronized void addAnnotation (Annotation a) {/*...*/} public synchronized Annotation nextAnnotation (boolean restart) {/*...*/} } Real-Time Java Programming

Christopher D. Gill Java: Synchronization Issues // Concurrency safety additions using block synchronization public class AnalysisPipeline { /*... */ protected void sendAlerts () { synchronized (al) {/* Send all the alerts in the list, reset alert list */} } public void run () { for (;;) { DataFeedEvent dfe = df.pullDataFeedEvent (); // spawns separate threads for long latency activities cf.handleDataEvent (dfe, al); } Real-Time Java Programming

Christopher D. Gill Java: Synchronization Issues // Concurrency safety additions using block synchronization public class PortfolioBalanceFilter extends AnalysisFilter { protected Portfolio p; public boolean handleDataEvent (DataFeedEvent dfe, AlertList al) { boolean consumed = false; synchronized (al) { /* add alerts based on data feed event and the projected risk and gain changes to portfolio */ } return consumed; } Real-Time Java Programming

Christopher D. Gill Java: Synchronization Issues // Concurrency safety additions using block synchronization public class SectorPEFilter extends AnalysisFilter { private NasdaqStore nh; private ResearchStore rr; public boolean handleDataEvent (DataFeedEvent dfe, AlertList al) { boolean consumed = false; /* compare PE to the average for its sector */ synchronized (al) { /* look at existing alerts*/ } /* possibly generate new ones, annotated in a separate thread with relevant research reports... */ synchronized (al) { /* add any new alerts to the list */ } return consumed; } Real-Time Java Programming

Christopher D. Gill Threads and Synch Points AnalysisPipeline SectorPEFilterPortfolioBalanceFilterCompositeFilter MarketOrderAlertOptionAlert CallAlert DataFeedAlert PutAlert BuyAlertSellAlert AnalysisFilter AnalysisTool AnnotationDataFeedEvent NasdaqDataFeed NasdaqAnnotation ResearchAnnotation DataStore NasdaqStoreResearchStore Portfolio low latency high latency medium latency AlertList AnnotationList Synchronization points: AlertList AnnotationList Real-Time Java Programming

Christopher D. Gill The RTSJ and Real-Time Issues Threads (revisited) Release characteristics & failures Scheduling Synchronization (revisited) Time and timers Asynchronous event handling Memory management Asynchronous transfer of control Exceptions System-level options Real-Time Java Programming

Christopher D. Gill RT Issues: Threads Multi-threading is useful to decouple different activities –Active objects, request queues, synch/asynch Must ensure resource usage by non-critical activities does not interfere with needs of critical activities However, work in different threads competes for CPU time and memory resources Real-Time Java Programming

Christopher D. Gill RTSJ: Threading Issues Threads compete for time on the CPU Some activities are higher priority than others Java thread priorities take us a step in the right direction, but… – garbage collector thread priority and preemption issues –Non-RT priority uniqueness is not ensured // Solution: real-time threads AlertThreadAdapter alertAdapter = new AlertThreadAdapter (ap, 1000); javax.realtime.RealtimeThread alertThread = new javax.realtime.RealtimeThread (alertAdapter); javax.realtime.RealtimeThread pipelineThread = new javax.realtime.RealtimeThread (ap); alertThread.start (); pipelineThread.start (); Real-Time Java Programming

Christopher D. Gill RTSJ: Threading Issues // To run the pipeline in a Realtime thread, it could just implement Runnable: for AnalysisPipeline this is not very invasive so we’ll skip writing a separate adapter public class AnalysisPipeline { /*... */ protected void sendAlerts () { synchronized (al) {/* Send all the alerts in the list, reset alert list */} } public void run () { for (;;) { DataFeedEvent dfe = df.pullDataFeedEvent (); // spawns separate threads for long latency activities cf.handleDataEvent (dfe, al); } implements Runnable Real-Time Java Programming

Christopher D. Gill RT Issues: Release Characteristics To know whether threads will interfere, need to characterize their temporal behavior Time execution cost periodminimum inter-arrival spacing deadline Can abstract out separate descriptors for canonical behavioral classes –I.e., periodic, aperiodic, sporadic Need descriptors with key temporal attributes –E.g., execution cost, deadline Real-Time Java Programming

Christopher D. Gill RTSJ: Release Characteristics Issues While threading allows priority partitioning, specific information and/or constraints on threads are needed Must ensure sufficient resources are available and correctly managed for desired behavior javax.realtime.RelativeTime cost = new javax.realtime.RelativeTime (100, 0); javax.realtime.RelativeTime period = new javax.realtime.RelativeTime (1000, 0); javax.realtime.PeriodicParameters pp = new javax.realtime.PeriodicParameters ( null, // start immediately, period, cost, null, // deadline = period end null, null); alertThread.setReleaseParameters (pp); alertThread.start (); Real-Time Java Programming

Christopher D. Gill RTSJ: Release Characteristics Issues // Analysis Tool Code, Revisited public class AlertThreadAdapter implements javax.realtime.Schedulable { /* we can & should get/set release parameters, scheduling parameters, memory parameters,... */ public void run () {addToFeasibility (); javax.realtime.RealtimeThread t = (javax.realtime.RealtimeThread) Thread.currentThread (); for (;;) { t.waitForNextPeriod (); // respect advertised cost, period times pipeline.sendAlerts (); } Real-Time Java Programming

Christopher D. Gill Release characteristics advertise how threads are projected to behave Time actual execution cost RT Issues: Release Failures deadline execution finished (late) projected execution cost However, differences between projected and actual behavior can lead to unexpected failures Need to be able to detect (and if possible handle) release failures –Cost overruns –Deadline misses Real-Time Java Programming

Christopher D. Gill RTSJ: Release Failure Issues Differences between projected and expected behavior result in release failures –Execution overruns –Deadline misses Can install a handler for each release characteristics instance to at least record, and possibly correct, failures public class CostOverrunEventHandler extends javax.realtime.AsyncEventHandler { public void handleAsyncEvent() {/*... */}} public class DeadlineMissEventHandler extends javax.realtime.AsyncEventHandler { public void handleAsyncEvent() {/*... */}} javax.realtime.PeriodicParameters pp = new javax.realtime.PeriodicParameters (null, // start immediately, period, cost, null, // deadline = period end new CostOverrunEventHandler (), new DeadlineMissEventHandler ()); alertThread.setReleaseParameters (pp); alertAdapter.setReleaseParameters (pp); alertThread.start (); Real-Time Java Programming

Christopher D. Gill RT Issues: Scheduling Priorities –Need sufficient unique priority levels Preemptive scheduling –Need well defined and appropriate semantics Fairness among threads is not usually a Real-Time concern (FIFO vs. RR) –But may be useful Feasibility –Admission control, certification/testing scheduler blocked runnable executing Real-Time Java Programming

Christopher D. Gill RTSJ: Scheduling Issues Release characteristics give control over threads Scheduling addresses how to manage those threads Priority, preemption Feasibility // Analysis Tool Code, Revisited javax.realtime.PriorityScheduler psched = (javax.realtime.PriorityScheduler) javax.realtime.Scheduler.getDefaultScheduler (); javax.realtime.PriorityParameters high = new javax.realtime.PriorityParameters (psched.getMaxPriority ()); javax.realtime.PriorityParameters med = new javax.realtime.PriorityParameters (psched.getNormPriority ()); try { alertThread.setSchedulingParameters (high); pipelineThread. setSchedulingParameters (med); } catch (java.lang.IllegalArgumentException e) {/*... */} alertThread.start (); pipelineThread.start (); Real-Time Java Programming

Christopher D. Gill RTSJ: Scheduling Issues // Analysis Tool Code, Revisited public class StoreThreadAdapter implements javax.realtime.Schedulable {/*... */ public void run () { javax.realtime.PriorityScheduler psched = (javax.realtime.PriorityScheduler) javax.realtime.Scheduler.getDefaultScheduler (); try { javax.realtime.PriorityParameters pp = new javax.realtime.PriorityParameters (psched.getMinPriority ()); setSchedulingParameters (pp); javax.realtime.RealtimeThread t = (javax.realtime.RealtimeThread) Thread.currentThread (); t.setSchedulingParameters (pp); } catch (java.lang.IllegalArgumentException e) {/*... */} store.annotateAlert (alert); } Real-Time Java Programming

Christopher D. Gill RT Issues: Synchronization Risk of unbounded priority inversions –Canonical high, low, middle scenario synchronized block blocked at guardrunning outside block waiting (blocked) on a condition variable running inside block priority key: highlowmiddle Priorities can uncover or exacerbate “bad” executions of existing race conditions –Horstmann & Cornell, ”Core Java 2” Need well defined thread and locking semantics Real-Time Java Programming

Christopher D. Gill RTSJ: Synchronization Issues Real-time threads at different priorities share resources However, this presents new real-time issues –Priority inversions Need additional mechanisms to ensure priority-safe sharing –Monitor Control Methods wait and notifyAll still work (avoid notify unless absolutely sure OK) –But, add overhead Non-blocking R/W queues: thread glue // Solution: Monitor Control javax.realtime.MonitorControl.setMonitorControl (new javax.realtime.PriorityInheritance ()); // Solution: wait-free queues public class StoreThreadAdapter implements javax.realtime.Schedulable { /*... */ private javax.realtime.WaitFreeDequeue dequeue; /*... */ } Real-Time Java Programming

Christopher D. Gill RT Issues: Time and Timers Time resolution needed –Hours down to nsec start expire Absolute time –Common temporal reference, e.g., UTC Occurrences over time Absolute clock Timer mechanisms –One-shot, periodic Relative Time –Since start of thread –Since last period Real-Time Java Programming

Christopher D. Gill RTSJ: Time and Timer Issues Threads offer a clean programming model However, many real- time systems benefit from asynchronous behavior Also, pacing is an effective/alternative way to reduce resource contention and improve resource utilization // A needed solution: watchdog timer public class StoreTimeoutHandler extends javax.realtime.AsyncEventHandler {public void handleAsyncEvent() {/*... */}} public class StoreThreadAdapter implements javax.realtime.Schedulable { public void run () { //... set up thread priorities... long m = 60000; // one minute new javax.realtime.OneShotTimer (new javax.realtime.RelativeTime (m,0), new StoreTimeoutHandler ()); store.annotateAlert (alert); } //... } Real-Time Java Programming

Christopher D. Gill RT Issues: Asynch Event Handling Threads allow synchronous programming styles Sometimes, asynchronous styles are more appropriate –Real-world timing issues –Decoupling processing handler event handler method Events-and-handlers model provides mechanisms for: –Synchronous –> threads –Asynchronous –> timers –Mixed –> half-synch / half- asynch pattern Real-Time Java Programming

Christopher D. Gill RTSJ: Asynch Event Handling Issues We saw an earlier example of a one- shot timer used to determine when a long-running thread had been gone too long Could also use a periodic timer to re- implement the high priority alert transmission code // Another way to implement periodicity public class TransmitTimeoutHandler extends javax.realtime.AsyncEventHandler {public void handleAsyncEvent () {/*...*/}} new javax.realtime.PeriodicTimer (null, new javax.realtime.RelativeTime (1000, 0), new TransmitTimeoutHandler ()); Real-Time Java Programming

Christopher D. Gill RT Issues: Memory Management Bounded allocation times Managed vs. raw access –Trade-off in control vs. responsibility Memory lifetimes –Program, local scope Resource use descriptors memory manager Application/manager interactions –Priority inversions –Memory contention Safety and liveness Real-Time Java Programming

Christopher D. Gill RTSJ: Memory Management Issues Realtime threads get higher priority than the garbage collector However, there is still a possibility of priority inversion –If GC is collecting the heap, it must reach a “safe” state before RT threads can use the heap NoHeapRealtime threads avoid this // Solution: separate memory areas and // no-heap real-time threads javax.realtime.MemoryArea ma = new javax.realtime.LTMemory (initSize, maxSize); javax.realtime.NoHeapRealtimeThread alertThread = new javax.realtime.NoHeapRealtimeThread (sp, // sched params rp, // release params mp, // memory params ma, // memory area pg, // processing group alertAdapter); Real-Time Java Programming

Christopher D. Gill RTSJ: Memory Management Issues // Immortal Memory is a Singleton javax.realtime.MemoryArea im = javax.realtime.ImmortalMemory.instance (); im.enter (this); // this must be Runnable // allocates memory on // the ImmortalMemory area // until another memory // area is entered, or // the Runnable run () // call exits and enter () // returns Scoped memory is key for no-heap real- time threads Other kinds of MemoryArea –Immortal Memory: can improve GC performance Physical Memory –Immortal, scoped, raw –Factory Real-Time Java Programming

Christopher D. Gill RT Issues: Asynch Transfer of Control Want to provide real-time behavior for long-running synchronous activities (e.g., searches) For fault-tolerance, some activities may need to be halted immediately However, standard threading and interrupt semantics can produce undefined/deadlock behavior in many common use-cases ATC refines semantics Publisher Exhaustive Lookup Shipper “find anything relevant” “stop and give me what you have found so far” searching Real-Time Java Programming

Christopher D. Gill RTSJ: ATC Issues Even with the one-shot timer, the long running- thread must be reigned in somehow Deprecated Thread stop, suspend calls are unsafe ATC defers exception as pending in synchronized methods – avoids problem w/deprecated Thread stop method // Data Store Query Code, Revisited public abstract class DataStore { /*... */ public abstract void annotateAlert (Alert a) throws javax.realtime.AsynchronouslyInterruptedException; } // In timer handling for // StoreThreadAdapter run () t.interrupt (); Real-Time Java Programming

Christopher D. Gill RT Issues: Exceptions Additional special-purpose exceptions w/ standard semantics for –Memory management –Synchronization –System resource management safe scope unsafe scope “tunnels” propagates caught (re)thrown Special semantics for ATC –When to throw (or not) –Deferred propagation semantics (“exception tunneling”) - safety –Nesting/replacement raised Real-Time Java Programming

Christopher D. Gill RTSJ: Exceptions Issues Semantics for AIE are different than others –deferred in pending state until inside a safe scope, where it will be thrown Other new exceptions deal primarily with incompatibilities of memory areas –Trying to assign a reference to scoped memory to a variable in immortal or heap memory –Setting up a WaitFreeQueue, exception propagation, etc. in an incompatible memory area –Raw memory allocation errors (offset, size) –Raw memory access errors Real-Time Java Programming

Christopher D. Gill RT Issues: System-level Options Although strict layering is often desirable, platform- specific issues tend to peek through –E.g., signals, schedulers Collecting the system-wide constants, methods, etc. under one or more classes reduces pollution and improves the programming model May add points of configurability (I.e., various system-wide managers) SIGKILL SIGINT SIGABRT security manager getManager setManager Real-Time Java Programming

Christopher D. Gill RTSJ: System-level Options Issues javax.realtime.RealtimeSystem is analogous to java.lang.System –Gives access to real-time system properties E.g., concurrent locks, endian properties –Allows a RealtimeSecurity manager to be set as the system security manager –Gives access to the current garbage collector PosixSignalHandler –Required on platforms that provide POSIX signals –Thus, can only be used portably among those implementations Real-Time Java Programming

Christopher D. Gill Review: Time Scales AlertList MarketOrderAlertOptionAlert CallAlert Alert PutAlert BuyAlertSellAlert AnalysisTool DataFeedEvent ResearchAnnotation ResearchStore AnnotationList Portfolio Low MediumLatency:High AnalysisPipeline SectorPEFilterPortfolioBalanceFilterCompositeFilter DataFeed AnalysisFilter NasdaqDataFeed NasdaqAnnotation DataStore NasdaqStore Annotation AnalysisTool ResearchAnnotation ResearchStore Real-Time Java Programming

Christopher D. Gill Review: Java, RTSJ, Real-Time Issues Threads (Java, revisited in RTSJ) Release characteristics & failures Scheduling Synchronization (Java, revisited in RTSJ) Time and timers Asynchronous event handling Memory management Asynchronous transfer of control Exceptions System-level options Real-Time Java Programming

Christopher D. Gill Review: Java and RTSJ AnalysisPipeline SectorPEFilterPortfolioBalanceFilterCompositeFilter MarketOrderAlertOptionAlert CallAlert DataFeedAlert PutAlert BuyAlertSellAlert AnalysisFilter AnalysisTool AnnotationDataFeedEvent NasdaqDataFeed NasdaqAnnotation ResearchAnnotation DataStore NasdaqStoreResearchStore Portfolio low latency high latency medium latency AlertList AnnotationList Synchronization points: AlertList AnnotationList real-time periodic no heap feasibile scoped memory priority inheritance high priority over-run handler duration timer aynch transfer of control Real-Time Java Programming

Christopher D. Gill Concluding Remarks The RTSJ extends and/or refines existing Java semantics to address issues of real-time concern –Priority control, memory management, release parameters, feasibility, … However, the RTSJ largely stays within the existing programming model –Some new idioms to master, but much is preserved –ATC in particular illustrates the trade-offs Stay tuned, more evolution is on the horizon –Reference implementations and benchmarking –New specification efforts, e.g., the DRTSJ (JSR 50) Real-Time Java Programming