Introduction to Real Time Java - Part 1

Tagged:  
No votes yet

Recently I attended a seminar on Real-Time Java by Eric Bruno. It was informative seminar about real time systems and their uses and where Real-Time java comes in to the picture with some examples and demos. So lets get in to details


What is Real-Time?

Real-Time means the ability to reliably and predictably respond to a real-world event. So real-time is more about timing than speed.Imagine situations where GC interrupted your threads execution and how many times you tried to fine tune GC parameters

Note: Read about fine-tuning java garbage collection performance

Real-Time is all about response time and how mission critical your application is. An application controlling the rudder of an airplane, for example, must not be delayed for any reason because the result could be catastrophic.

Challenges for Real-Time System

Java SE got so many hurdles to act like a real-time system viz. Thread Management, Class Loading, Just-In-Time Compilation and GC ( Garbage Collection ). Lets have a look at these hurdles in detail.

Thread Management

Java SE doesn't guarantee about priority and scheduling. A low-priority thread may be scheduled in front of high-priority thread making it to wait for certain time.

Class Loading

Java SE doesn't load a class until it is referenced for first time in program. Loading a class involves certain time depending on the size of class, loading parents of the class, overhead of class loaders etc. All this delay will add upto minimum of 10 milliseconds and in real-time system we can't afford to loose that time. Careful application design can be used to load all classes at application start-up, but this must be done manually.

Garbage Collection

GC is a boon as well as curse for java programmers. GC benefits application development by including pointer safety, leak avoidance, and freeing developers from needing to write custom memory-management. On the other hand, traditional garbage collectors can introduce long delays at times that are virtually impossible for the application programmer to predict. Delays of several hundred milliseconds are not unusual.

Compilation

Compiling Java code to native code introduces a similar problem as class loading. Most JVMs initially interpret Java methods and, for only those methods that execute frequently, later compile to native code. Delayed compiling results in fast start-up and reduces the amount of compilation performed during an application's execution. But performing a task with interpreted code and performing it with compiled code can take significantly different amounts of time. For real-time systems this is unacceptable as it demands immediate response.



Real-Time Java Specification

RTSJ (JSR-001 & JSR-282) was created to address some of the limitations of the Java language that prevent its widespread use in RT execution environments. The RTSJ addresses several problematic areas, including scheduling, memory management, threading, synchronization, time, clocks, and asynchronous event handling.

Scheduling

Real-Time systems need to control strictly how threads are scheduled and guarantee that they're scheduled as defined. True priorities and fixed preemptive scheduler with priority inheritance support is required for real-time threads. This scheduling approach ensures that the highest-priority active thread will always be executing and it continues to execute until it voluntarily releases the CPU or is preempted by a higher-priority thread. Priority inheritance ensures that priority inversion is avoided when a higher-priority thread needs a resource held by a lower-priority thread.

Memory Management

Real-Time Specification defines immortal & scoped memory areas to supplement the standard java heap. These areas allow tasks to use memory with out being required to block if GC needs free memory in the heap.

  • Objects in immortal memory area are accessible to all threads and are never collected. Because it is never collected, immortal memory is very limited in resources and must be used carefully.
  • Scope memory areas can be created and destroyed under programmer control. Each scope memory area is allocated with a maximum size and can be used for object collection.


To ensure integrity of references between objects, real-time specification defines rules that control how objects in one memory area (heap,immortal and scope) can refer to objects in other memory areas. Because of these complexities, the recommended use of immortal and scoped memory is limited to components that cannot tolerate GC interruptions.

Threads

Real-time specification adds support for two new thread classes that provide real-time behavior: RealTimeThread and NoHeapRealTimeThread (NHRT). These classes support priorities, periodic behavior, event handlers and can use memory areas other than heap.
  • NHRTs are mostly not interrupted or preempted by GC. NHRTs are typically used for tasks with highest priorities.
  • RealTimeThreads used for tasks with latency requirements that can be accommodated by a GC.
  • Regular Java Threads for everything else
Synchronization

Synchronization must be carefully managed within a RT system to prevent high-priority threads from waiting for lower-priority threads. The RTSJ includes priority-inheritance support to manage synchronization when it occurs, and it provides the ability for threads to communicate without synchronization via wait-free read and write queues.

Time and Clocks

Real-Time systems need higher-resolution clocks than those provided by standard Java code. The new HighResolutionTime and Clock classes encapsulate these time services.

Asynchronous event handling

Real-Time systems often manage and respond to asynchronous events. The RTSJ includes support for handling asynchronous events triggered by a number of sources including timers, operating-system signals, missed deadlines, and other application-defined events.

Part - II follows. Please leave your comments and suggestions in comments.

Post new comment

CAPTCHA
This question is for testing whether you are a human visitor and to prevent automated spam submissions.
X
P
s
b
L
d
Enter the code without spaces and pay attention to upper/lower case.