Realistically real-time

Real-time Java application development using multicore systems

Page 4 of 4

Closures and the ConcurrentContext

Currently several proposals exist for adding closures to Java 7. My preference goes to the Concise Instance Creation Expressions (CICE) proposal championed by Joshua Bloch. Not only is this proposal the simplest but it would work quite well with Javolution constructs such as ObjectFactory or ConcurrentContext. This proposal is a natural syntax simplification of anonymous classes with single abstract method (such as ObjectFactory.create() and Runnable.run()). Listing 2 shows what the concurrent FFT calculations rewritten using CICE closures would look like.

Listing 2. Concurrent FFT calculations with closures

enter();
try {
    for (int i = 0; i < n; i++) {
         final int j = i % K;
         execute(Runnable() { fft(frames[j], results[j]); });
    }  
} finally {
    exit(); // Wait for concurrent execution to complete.
}

I would also like to suggest that the closure class type be omitted when it can be inferred; for instance: execute(fft(frames[j], results[j]); });.

Class initialization

The Java platform specification is very clear about when and how Java classes are initialized. Class initializers are run at first use either when an instance of the class is created or when static methods (or fields) are called. In case of static class circularity, initialization order (and calling order) matters as it can result in an illegal state (such as static final member not set). Listing 3 is an example of illegal state.

Listing 3. An example of illegal state

class> A {
    public static final A ONE  = B.ONE;
 }
 class B extends A {
    static final B ONE = new B();
 }

If a program uses B before A, then ONE is null.

But the main problem for a real-time system is that the execution of a new conditional branch may cascade into hundreds of classes being initialized at an inappropriate time. There is no easy solution to this problem without changing the Java specification. One possibility, which is being employed on large real-time servers, is to force the initialization of all classes at startup. This can be done through the Javolution command: ClassInitializer.initializeAll(). But user beware: this command may take several minutes as it initializes all the classes in your classpath including the runtime library classes.

Real-time libraries

All the techniques described here, and even the best real-time virtual machine, are immaterial when non time-deterministic libraries are employed. What is the point of having concurrent garbage collection if a simple call to add one element to a standard Java collection results in delays in the hundreds of milliseconds because the collection has resized itself internally?

It is very unlikely that the standard Java libraries will be rewritten to become time-deterministic. A solution is to use external libraries having bounded/predictable response times. The Javolution collections implement the standard collection interfaces and can be used as drop-in replacements of the standard Java utility classes. Time-deterministic behavior is achieved through incremental capacity increases instead of full resizing. In other words, resizing occurs more often but has less impact on execution time or memory fragmentation.

Execution time of List.add(Object)
Figure 8. Execution time of List.add(Object)

In many instances, these classes are as fast as their standard library counterparts, proving that you can be both real-time and real fast!

In conclusion

Multicore processors are a godsend to Java developers, having significantly increased the field of applicability of the Java platform. As demonstrated with the FFT case study in this article, using multicore processors makes it possible to achieve a high level of time-determinism on a standard virtual machine. You simply have to know how to configure and optimize the VM and, most importantly, limit the garbage flow of your application.

Will you be at JavaOne? Follow up this article by attending author Jean-Marie Dautelle's presentation on fully time-deterministic Java technology at JavaOne 2008.

Jean-Marie Dautelle holds a master's degree in electrical engineering (Orsay University, France) and a postgraduate degree in information processing and simulation. He is spec lead for JSR 275 and has been a Java Executive Committee member since November 2006. Jean-Marie works for Raytheon in Marlborough, MA on Java-based technology for safety-critical systems. Jean-Marie is the main author of the Javolution and JScience Project libraries.

Learn more about this topic

Technologies for real-time

| 1 2 3 4 Page 4