An Example of Programming in Mozart versus Java

The programs are simple producer/consumer examples. The producer/consumer example is a common design pattern in distributed computing. In the simple example the producer produces a stream of 1 million integers, from 1 to 1 million. The consumer consumes the list, incrementally summing the elements, and finally outputs the entire sum. In more realistic applications using the same design pattern the producer will produce complex data structures requiring some calculation invoking a more complex and varied behaviour in the consumer.

The producer and consumer execute in different threads, possibly on different computers. The only synchronization is data-flow. The producer may outpace the consumer and produce elements faster than the consumer can deal with them. In this case the elements are buffered. However the consumer cannot outpace the producer and must wait if no next element is available.


In this directory you will find Mozart and Java source files for the centralized case. Here the threads run in the same process.
Java Mozart
Execution time 17.6 seconds 3.9 seconds
Lines Code 108 28
Figure 1: Statistics (jdk 1.2 and mozart 1.0.1 on solaris UltraSparc II)

Comments: In general Mozart and Java have comparable performance. But Mozart has superior mechanisms for data-flow and multi-threading both of which are important in this application.


In this directory you will find Mozart and Java source files for the distributed case. Both are distributed straightforward by the recommended method. In Mozart the stream data-structure is published in ticket form on a file or web page by the producer and then downloaded by the consume. In Java distribution is achieved with RMI (Remote Method Invocation).
Java Mozart
Execution time 1 hour 8.0 seconds
Lines Code 220 32
Figure 2: Statistics

Comments: The main reason that Java is so much slower is that for each element the producer produces a network message will be sent and the thread synchronizes on the return of the rmi call. The Mozart system will automatically gather together groups of elements that the producer has produced and thus sends less messages across the net. In addition in Mozart the producer does not need to synchronize only the consumer.

The Java program can, of course, be optimized for the given distributed structure, but this makes the program larger and more complicated. Also, the distributed program would differ from the centralized program even more, demonstrating that efficient distributed programming is considerably more difficult in Java than in Mozart. Note that the only difference between the centralized and distributed program in Mozart is that the provision for sharing the stream between the two processes, consumer and producer. The rest of the program is identical.

Try Yourself

You may want to try these examples for yourself. Copy the contents of the directory and read more about how to run the centralized bench and the distributed bench
main  | centralized  | distributed