Java has long been one of the central technologies of enterprise applications. The speed and scalability of the JVM, in particular, have endeared it to large IT organizations. But today, companies need more than just fast performance; they are increasingly searching for deterministic, real-time performance.
Determinism in this sense means that a given action will occur within a fixed time interval, such as delivery of a stock quotation within some number of microseconds. Historically, Java has not been used to fill that role, because of some early design decisions in the platform. However, new options and new technologies are enabling IT organizations to use Java for both standard business needs and situations where deterministic, real-time requirements must be met.
Few things generate more confusion in the programming world than discussions of real-time software. Many confuse it with high-performance computing, while others use it to describe any system that pushes data to the user without a user request. These characterizations alone are simply not accurate. While it's true that both these aspects may be part of real-time system behavior, the definition of a real-time system centers on one word: time. Correctness means producing the right answer, and doing so at a precise moment in time.
Another source of confusion is the difference between hard and soft real-time systems. A hard real-time requirement is one in which a task needs to be complete at or before a certain time, every time it's required, regardless of other factors. A soft real-time system contains some tolerance in terms of missing its deadline.
For example, stating that a foreign exchange trade needs to settle within two days is a hard real-time requirement; whereas stating that video player software needs to update its frame sixty times per second is a soft real-time requirement — occasionally dropping a frame isn't considered an error. However, it's still a real-time system because too many dropped frames, or too much delay between them, is considered an error.
Java and Real-Time Development
Java Standard Edition (Java SE) is not ideally suited for real-time requirements. Existing Java virtual machines (JVMs) are just not designed for predictability and determinism.
For instance, garbage collection (GC), in which the RAM allocated to no-longer-used data items is reclaimed for reuse by the JVM, is one source of trouble. GC can occur at any time, for any length of time, with few options for users to control it. This potential for unbounded delays makes it impossible to guarantee a system's behavior; it is non-deterministic. Attempts to pool objects or somehow control their lifetime to avoid GC are often in vain, as GC pauses may still occur. However, Java SE's real-time deficiencies go beyond the garbage collector. The just-in-time (JIT) HotSpot compiler — the same one that compiles bytecode to machine code — is also non-deterministic. Because JIT compilation can occur when your code is executing, and can take an unknown length of time to complete, you can't be guaranteed that your code will meet its deadlines all the time. Even Java classes that have been JIT-compiled are subject to reoptimization at any point in the future. More importantly, Java provides no guaranteed way to prioritize threads or event handling within your application code. Therefore, even if GC and JIT could be controlled, real-time behavior could not be guaranteed without the ability to prioritize your thread processing. With strict priority control comes the need for advanced locking beyond what most standard JVMs provide. These are important points to remember, as most people blame the GC and JIT compiler entirely for Java's lack of real-time ability.
The C++ Alternative
A common alternative to Java for real-time development is C++, but this is a flawed solution. While C++ is a good language, it is not an entity that magically yields predictability and determinism. It requires great skill and enormous knowledge, along with operating-system support and integration, to deliver a real-time system in C++.
For instance, although GC problems don't exist in C++, the C runtime on which C++ depends for heap management can exhibit non-deterministic behavior. Quick examination of some standard C runtime library code reveals that locating free memory chunks for use by the program can involve extensive memory manipulation. This can lead to unpredictable results during memory operations.
Instead, with C++, the developer bears a great burden to ensure deterministic, predictable execution across all aspects of application processing. This usually results in a dependence upon third-party libraries for memory management, thread processing, and interaction with the operating system for I/O operations. These are all features that the Java VM provides built-in. Used correctly, the JVM can perform these operations while providing support for real-time development. Here's how.