Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
Download as pdf or txt
Download as pdf or txt
You are on page 1of 8

IEEE TRANSACTIONS ON NUCLEAR SCIENCE, VOL. 45, NO.

4, AUGUST 1998 1965

Java in Real-time Applications

Edy Bertolissi and Clive Preece


School of Engineering, University of Durham. South Road. Durham. DHl 3LE. U.K
edy.bertolissi@durham.ac.uk clive.preece@durharn.ac.uk

Abstract Java presents several new and positive features. and it is


clear that it is much more than a language for adding
This paper addresses the use of the Java programming
animations to Web pages. The next sections will give an
language for real-time applications, giving an overview of its
overview on the main advantages and current limitations in
main advantages and current limitations. Java can implement
the use of Java in embedded and real-time systems, and
at language level several of the constructs defined by the
examples of projects involving real-time applications of Java
POSIXl .b standard for real-time applications. However, if
will be discussed, together with comments on appropriate
Java is to realise its potential in this field, appropriate
extensions to the language.
additions to the language will need to be made, and further
work will be necessary.
11. JAVAFOR THE REAL WORLD
I. INTRODUCTION In programs such as editors and compilers the element of
time may not be critical to their operation. There are large
Java, a programming language developed by Sun classes of applications which are involved with time and
Microsystems, represents a new candidate for a robust,
deadlines, requiring to a greater or lesser degree some
architecture independent programming language. Although
elements of real-time performance. There are two distinct
originally developed for consumer electronics, it is becoming classes of applications, those which interact just with the
well known as the language for programming applets, small computer and its peripherals, and those which need to interact
executable programs which can be embedded into .an HTML also with the world outside the computer. Examples of the
page on the World Wide Web. Even if the main use of Java is first class of programs can be found in the increasingly
related to the Internet world, it still retains the characteristics expanding market of multimedia systems, teleconferencing
of a programming language for consumer electronics. The and video games. Of the second class, aircraft control
software for these types of applications must have special systems, or real-time digital diagnosis systems are typical
features: it must be able to be ported to different architectures applications which interact with the external world. These are
and processors without changes even though the underlying often implemented as embedded systems on chips which form
hardware is up-graded, and it must have a high degree of a component of the greater system.
reliability to avoid failure of the device with possible
hazardous consequences. One of the main advantages of Java consists in the fact
that it is a platform independent language. Developers can
Java has several features which may be advantageous in a write an application on one platform and then run it on any
language to be used for real-time development. It is a simple, platform which implements a Java Virtual Machine. This
easy to learn and use, object oriented programming language. eliminates the problems associated with cross-compiler
It defines only a limited number of language constructs which development systems, multiple platform version maintenance
are similar to those used in C or C++. It has eliminated and rewriting and retesting the software every time it is ported
several of the features of C and C++ which tended to to a new processor [l]. This fact is extremely attractive since
encourage programming errors such as pointers. It is designed
programs such as video games could be developed on one
to be suitable for use in distributed applications on networks, platform and then be ready to be commercialised for a whole
allowing easy access to remote resources. It is robust; it is a range of computers. In the fast moving scene of embedded
strongly typed language which performs several run time
systems where new and improved chips are introduced
checks, such as that of array access within the bounds. It has
frequently on the market, applications which could be simply
built-in multi-thread support and the ability to load ported from one chip to another offer immediate advantages.
dynamically classes as they are needed. It has been developed Old silicon could be quickly replaced without a large
with the aim of creating a programming environment which investment of effort in adapting the software to the new chip.
makes programming errors less likely to occur. It is designed The platform independence feature of Java does present some
as an interpreted language, and this means that the code
complications in time critical applications as developers may
generated by the compiler is architecture independent and can
not be able to test their applications in each environment
nin on any system which implements the Java Virtual
where they are expected to run. A real-time application would
Machine. Recently, however- alternatives to interpreted Java have to run within the same constraints on platform with
have been developed, and Just in Time (JIT) compilers and different features and speed (for example a 33 MHz Intel
Java compilers have been introduced. The language standard
40486 and a 300 MHz Digital Alpha) [I]. Moreover it is
is maintained as Sun Microsystems reserves the right to alter possible to run the application as a process on a time shared
it.

0018-9499/98$10.00 0 1998 IEEE


1966

operating system where it would compete with the other tasks the time of writing full details have not yet been released At
for the CPU time present the only solution to this problem is to take advantage
Clearly the expectation of using Java as the solution for of the feature o f Java which permits the inclusion of methods
implementing real-time programs which are able to meet the written in native code into Java programs This means that it
same deadlines on any platform is unreasonable. This is is possible to write procedures which access the hardware in
especially true when hard real-time performances are some local native system language, typically C or C++, and
required It cannot be assumed that a real-time application include them in the Java application These methods cannot
which just manages to achieve its goals on a fast machine provlde the security guarantees typical of a Java program
such as a Sun Ultra would be able to provide the same Moreover native code cannot guarantee portability, and any
behaviour on a Intel 486 platform. In many applications the Java code that relies on native methods must be ported to each
development of a real-time system must be carried out closely type of platform on which that code is to run.
allied to the underlying hardware since performance must be Other useful Java features include its fixed-size longs,
verified and guaranteed to meet constraints. It is important to ints, shorts and bytes which allow the programmer to write
recognise that a language that can run on any platform does more portable code, although the lack of unsigned types may
not mean that it will be able to produce the same time be a problem in some cases.
performance on all of them. At a higher level, Java allows change of the code “on the
The term Java IS used to identify three different entities fly” since Java classes can be dynamically loaded into the
e Java the language the programming language; application. This feature is especially useful for systems which
e Java the Bytecode. the outcome o f the compilation of a need a lot of flexibility At the present this capability is used
Java program; for loading applets across the Internet The ability to add new
Java the Virtual Machine (JavaVM)’ the application applets or update old ones on the fly is extremely powerfid,
required to run the Java Bytecode on a specific platform. and can add to the functionality of any system easily and
quickly Users can acquire new functionality or new user
While the first two entities are strictly defined by Sun
interfaces by simply connecting to the local server or to the
Microsystems and any alteration in them would result in the
network This is becoming widely used in multimedia
creation of a non-standard dialect of Java, the Java Virtual
applications However using the same facility in safety critical
Machine is not completely specified. a fact which leaves some
system might raise security issues. In addition the design of
freedom for implementation
some embedded systems might require changes to the
hardware to cope with new software features loaded in this
III. REAL-TIME FEATURES way It is nevertheless a powerful capability and one which
While the platform independence of Java is a great could be exploited by systems which have a server or network
advantage of the language, there are other positive features connection
which are desirable in a language which is to be used for real- Java’s built in support for multithreading and multitasking
time development. represents an advantage during the development of real-time
First Java allows easier code development since it is applications However the capability of predicting the
object oriented, but without some of the complexity of C++ behaviour of an application written in Java in a multitasking
Its platform independence permits initial code testing and environment requires support from the run-time system. One
debugging on other host platforms before starting work on the possibility of ensuring predictable behaviour consists in
target machine developing a JavaVM which takes advantage of the features of
Another important advantage IS in memory management a real-time operating system (RTOS) In this context Java
Java relies on automatic garbage collection while C and C++ with its VM is analogous to C or C++ running on an RTOS
generally require the programmer to free up unneeded kernel. These other languages provide support for
memory Memory leaks (and, worse, freeing an object which multitasking but not in the integrated way that it is done in
IS still being referenced) are some of the most common Java. At present the developers of the RTOS QNX have
programming errors expressed their intention to develop a JavaVM which will run
on their operating system. However commercial RTOS’s
Java’s type safety is another feature which decreases the generally provide a bider range of features for process control
likelihood of programming errors. However it prevents direct than Java and its class libraries, and therefore some additional
access to hardware registers, as is done in C and C++ by the code would have to be written to make Java competitive in
use of casts Allowing Java to access the underlying hardware this market. Perhaps if web applets were not so important a
would produce two types o f problems. First of all it would part o f current Java applications, this is a dxection that the
raise security issues, and second it would prejudice platform language might have taken. At present the standard
independent features. This could be seen as a problem in POSIX l b [2, 31 defines a set of support functions for the
embedded applications where it is necessary to access the development of real-time applications in real-time
hardware However Sun Microsystems has announced the multitasking environments. If Java is to be used for
development of a Java version for embedded systems, but at
1967

developing real-time systems it has to provide a set of defeat one of the main reasons of using shared memory,
fiinctions comparable to the ones defined in the standard. The namely fast access to data. Since it is impossible to implement
files < u n i s t d . h> and d i m i t s . h> describe the operating shared memory in Java at present the problem of
system's POSIX support at compile time. Since Java is synchronising multiple processes for accessing data will not
supposed to be machine independent the local configuration be apparent. If a process needs to read data held in another
of the system should not be an issue of the application, but process the second one can decide when to provide the
eventually only for the JavaVM. A function for checking the information, thus eliminating the problem of generation of
run time environment, like s y s c o n f defined in POSIX, does partially updated data. POSIX provides a set of functions for
not make sense in the Java language for the same reason. The setting the scheduling policies of the processes. Since Java
problem of namespace pollution addressed in POSIX is (the language) has no control over the OS where the
eliminated in Java since there are no global variables or application is running, it is not possible to define a similar set
fiinctions, and a unique package naming scheme that is based of facilities. A partial solution to this problem could be
on the domain name of the organisation where the package implemented at the level of the JavaVM. It could be written in
has been developed has been proposed [4]. Java allows the such a way that it would be possible for it to define the
creation of child processes by calling the method scheduling policy and the priority of the process which is
Process.exec ( ) . The object returned by the exec ( ) going to run when it is started. In this way no alteration of the
call provides methods to access the input, output and error language would be necessary, but the solution would not
streams of the child process. In addition methods for waiting provide all the flexibility that the POSIX approach allows.
for the process termination, killing it or retrieving its exit Java has access to the system clock (with millisecond
value are provided. resolution). It provides basic timer facilities which could be
POSIX defines a series of mechanisms which allow the used inside the Thread class. These are not as sophisticated
communication and co-ordination of processes: signals, as the timers defined in POSIX, but they could provide
messages, shared memory, and synchronisation. Signals are sufficient support in many applications. Scheduling and
used for many purposes including exception handling, process timers are the means for making applications perform
notification of asynchronous event occurrence, process operations on time. However the OS may create some
termination in abnormal circumstances, emulation of obstacles to letting this happen. The first problem is related to
multitasking and interprocess communication. Java does not the performance of the OS. If the OS is too slow the only
have any support for signals, but several of the tasks solution to this problem is to use a faster OS andor platform.
implemented by signals can be done in other ways in Java, for The second problem consists in the fact that many modern
example by using the Java built in exception handling operating systems make extensive use of virtual memory. If
mechanism or threads. Signals for interprocess not enough physical memory is present the system performs
communication cannot be implemented easily (in any case automatic paging moving parts of physical memory onto hard
signals are not an efficient and reliable interprocess disk making new space available. In addition swapping may
communication system even if POSIX. l b gives them some occur which involves dumping the entire process out to disk
useful features). Messages are used to pass information and reusing its physical memory. The operating system
between processes in a more efficient way than signals. Three decides what and when paging or swapping occurs. The
different mechanisms for message passing are available; problem is that the time of accessing data which has been
pipes, FIFO and message queues. Pipes work like streams: transferred to hard disk takes an order of magnitude greater in
one process simply writes data at one end of the pipe, and the comparison with that when data is present in the physical
other one reads at the other end. A FIFO is simply a pipe with memory, and therefore the expected response time cannot be
a name. A message queue is a more sophisticated version of a guaranteed. The solution is to lock down data or the entire
FIFO since it consists of a priority queue of discrete messages. processes in memory. At the language level Java does not
In Java since it is possible to access the input and output allow this. However again the solution for locking entire
streams of the child process, the implementation of a pipe is a processes down in memory could be found at the level of the
simple operation. If it is necessary to prioritise the messages it JavaVM which could give the OS the instruction to avoid the
is possible to create a thread in the sending process which swapping out of memory of the executing process. Data
sorts the messages and forwards the proper order to the locking could be achieved by extending the meaning of the
receiving process. volatile keyword. At present a variable is defined as
The creation of named entities is a more problematic volatile if it changes asynchronously and therefore the
issue. In other situations, shared memory provides a low level compiler must read the variable's value from memory every
for processes to communicate with each other. Unfortunately time and not attempt to save its value in a register. The
since Java is not able to address memory directly it is not JavaVM could extend this definition by locking down in
possible to implement shared memory. A higher level solution memory data which is defined as vo 1ati 1e.
for implementing shared memory would consist in declaring POSIX defines support for synchronised YO. When I/O
common segments of memory within a process. However this operations are synchronised with the underlying device it
solution would rely on pipes for communication, which would means that they return only when the device is appropriately
1968

updated This is not always the case since several systems for option which is always kept to the minimum Since the code
efficiency reasons use a buffer cache to hold I/O for later of the JavaVM can be measured in hundreds of kilobytes its
flushing on disk This provides an increase in performance use is not a practical option in small applications The size of
These functions are not present in Java, but could be the JavaVM itself is of the order of 64 Kbytes, but standard
implemented at the level of the JavaVM. Unfortunately in this class libraries occupy a large amount of memory It is possible
case all the I/O operations (or classes of them) would be to think of implementing a JavaVM for embedded use which
treated as synchronised with associated performance does not include the libraries since they are not part of the
problems Since I/O synchronised methods are necessary to language, thus reducing the size of the runtime interpreter
access devices, the inclusion of a method written in native The implementation of a JIT compiler is a feasible approach,
code using the POSIX features would be a better solution but only for larger systems since it requires more memory to
Finally POSIX provides support of asynchronous YO, which allocate the Bytecode, the JIT compiler and the translated
means that I/O operations are executed in parallel with the native code, in comparison with an interpreter or the native
application Java allows this to be achieved using threads. code approach For applications targeted to be executed on
At the present Java lacks of some of the features required small dedicated systems it is most likely that the best solution
by the POSIX standard, but some of the problems could be is to compile to native code, while for applications which run
solved modifying the JavaVM However Ken Arnold and on more powerful machines the use of JIT compilers might be
James Gosling, the creators of Java, write [5] the preferred option since they do not require the developers
to compile the code individually for the specific platforms At
Each implementation of Java must provide one or the time Java was launched, Sun Microsystems announced the
more appropriate extended classes of P r o ce s s that introduction of a series of Java chps. microprocessors with
are able to interact with processes on the underlying different features, which could run Java Bytecode without the
system Such classes might have extended functionality need of a JavaVM Unfortunately Sun has not released the full
that would be useful for programming on the details on these chips and therefore their assessment is
underlying system The local documentation should difficult However the idea did not find enthusiasts in the area
contain information on this extended functionality. of embedded system since it is not clear how it would be
Therefore it is possible to expect some developments in possible to develop device drivers (which need to address the
the future in this part of the language, which may however hardware) using the current version of Java Moreover at the
undermine its platform independence. high end chip market it is difficult to see the advantages of a

Internet applications which must run on any platform, but it is At present Newmonics Inc is developing an ambitious
generally undesirable in other cases. However Java need not project for a clean-room implementation of Java especially
necessarily be used as an interpreted language, even if at designed for reliable embedded real-time systems [SI The
present implementations are based on interpreters There are proposed JavaVM implementation, called PERC, would allow
two other possible alternatives which increase performance loading and analysing of the Java code at run time When a
The first option involves the precompilation of Java to native new real time activity is loaded a configuration manager
machine code, After a product has been developed, the Java would assess the requirements of the code (execution time,
code is compiled directly to native machine code prior to memory requirements etc.). Then a resource negotiator would
shipping. At present there is only one example of a Java analyse the data from the configuration manager and decide if
compiler, Toba [6], developed at the University of Arizona, the new application can be accommodated. If this is not
but it is likely that their number will increase soon. The possible, other applications would be terminated to free
second option is to apply Just In Time (JIT) compilation This resources for the new one. If it is not possible to terminate any
type of compilation, known also as “on the fly” compilation, of the active applications the problem would be reported The
works in the following way Once a Bytecode has been loaded activities related to the timing analysis of the code and its
iiito a particular virtual machine environment it can be schedulability performed by the PERC are complex and many
translated at run time (by the virtual machine environment) people have raised doubts on the feasibility of the project It
iiito the host machine’s target instruction set just before the seems that the company aims as a first step to implement a
code is actually executed. An example of such type of JavaVM JavaVM for soft real-time applications and then to develop a
has been developed with the Kaffe VM [7].The use of a hard real-time version. However the majority of the critics of
JavaVM is problematic for embedded applications since they the project question the decision of the developers to introduce
are often are implemented on small cheap and power frugal two new keywords; the atomic statement for providing a
chips Memory on these systems is usually an expensive new type of synchronisation mechanism and the timed
1969

statement for defining the upper bound of CPU time allowed deallocate the instance of Java.lang .Class that
to the code included in this statement. The reasons for their represents the class j ava.lang. SecurityManager,
introduction are to provide greater efticiency in comparison thus:
with synchronised statements, to make the code more hypothetical-free(C1ass.forName
readable, and to simplify the development of the Bytecode ‘‘1ava. lang . SecurityManager“)) ;
verifier. It is believed that these extensions to the language are exposing the system to any type of attack from malevolent
dangerous since they will create a non-portable Java dialect. applications.
They also seem unnecessary since it would be possible to
In the specification of the Java Virtual Machine (JVM)
develop class libraries with equivalent semantics which, used
[ l l ] Sun Microsystems leaves the designer the freedom to
in conjunction with a run time environment able to interpret
instal the type of garbage collector required by the specific
their semantics, could provide the same functionality. The
Java implementation. Quoting from the document:
company recognises this since it plans to develop a compiler
which would translate extended code in plain Java (code or The Java language is designed to be garbage
Bytecode); however,.it is not known if it would be possible to collected - it does not give the programmer the ability
achieve the same performance. to deallocate objects explicitly. Java does not pre-
suppose any particular kind of garbage collection;
Another example of development of Java classes for the
various algorithms may be used depending on system
support of real-time applications has been done by James
requirements.
Young at the University of Berkeley [9]. He has developed
support for “real-time tasks”, which are directly analogous to Therefore the implementation of the garbage collector is
threads, except that they have attached to them some implementation dependent and any of the available
additional timing information related to worst case execution, algorithms may be used for its design. This freedom granted
expected execution time, and deadlines. It is possible to by Sun Microsystems has both problems and advantages.
specie hard real-time, soft real-time and non real-time tasks. Because the research in the area of garbage collectors is very
Tasks are co-ordinated by the JavaRealTime executive which active, and new and improved algorithms are often proposed,
chooses to accept new tasks or not. Unfortunately sometimes a tight specification would have precluded the JVM from the
browsers and JavaVMs running applications which make use new developments in this area. At the same time it allows the
of JavaRealTime terminate themselves. The package has been development of a garbage collector implementation which
proved to be very reliable on some platforms (such as Pentinm best suits the particular hardware (it may take advantage of a
133 with Win9.5, using Sun JDK 1.0.2), but immediately kills chip designed specifically to help the activity of garbage
the JavaVM on other platforms (for example DEC Alpha, collection). On the other hand it might again prejudice Java‘s
using Netscape 2.02). In other cases the behaviour is supposed platform independence; different implementation of
inconsistent and depends on the runs. Young presumes that the garbage collection may cause different behaviour of the
this may be due to the combination of bugs in the code of the programs.
JavaRealTime and bugs in Java VM implementations on the In a real-time environment the possibility of implementing
vendors’ part. any garbage collector makes it possible to choose one which is
able to satisfv the constraints of the system. The introduction
Iv. GARBAGE
COLLECTION of discrete memory management in Java is not necessary to
One of the main differences between Java and C/C++ achieve the performance needed for applications targeted for
consists in the fact that while in the latter languages objects the real-time market. The crux of hard real-time
are allocated and deallocated manually with the implementation is being able to assess worst case instruction
malloc/free or new/delete functions, Java uses timings to see whether during the execution of a process an
garbage collection. Garbage collection is a part of a incoming interrupt can be serviced within a specific interval
language’s runtime system, or an add-on library, sometimes of time. The ability to delete objects is neither necessary nor
sufficient to achieve this. Other requirements are:
assisted by the compiler, the hardware, the OS, or any
combination of the three, that automatically determines which a way to suspend the activity of the garbage collector
data objects are no longer in use, and recycles their memory within a guaranteed time;
space for reuse. A comprehensive review of the major deterministic primitive operations ( e g no virtual
techniques used for the implementation of garbage collectors memory),
is presented in [lo]. Java uses garbage collection and In order to be able to suspend the activity of the garbage
therefore frees the programmer from the problem of memory collector it is necessary to have a sufficient amount of Pree
management and eliminates a whole class of possible bugs in memow, or to be sure that the calling process does not
A prbgiam. In addition the other important benefit of garbage allocate new objects (although it may use preallocated ones).
collection is related to security. The presence of a free This constraint may be too tight and the preallocation of all
function cannot be allowed in a secure system since it would the possible objects may be too taxing. However traditional
make it possible to destroy an object at random. For example real-time operating systems use deterministic dynamic
with a free function it would be possible for a program to memory management schemes. These do things like
1970

maiiagiiig pools of fixed size blocks, so there is no which may produce harm to the system once downloaded
recoinbiniiig to do at any time They do not use either garbage and executed locally. This defeats the philosophy of the
collecting techniques or a new/delete or malloc/free language which aims to offer an environment where the
approach The approach of releasing a proportional amount of implementation of virus-like programs would be
ineinory every time a new object is allocated would avoid the impossible.
problem of memory starvation In addition it is necessary to Embedded real-time garbage collectors are already used
be able to disable the virtual memory whose access timing is in some applications. For example, the
not deterministic Telecommunications company Ericsson uses a functional
An argument in favour of garbage collection in real-time programming language, Erlang, in embedded real-time
systems is that in general most of the hard real-time telephony in conjunction with a very fast real-time
requirements in a control system are actually a few critical garbage collector.
operations, such as accurately tagging measurement data , say
from a variety of sensors and from the system clock Once v. JAVATHREADS
these operations are done, the rest of the processing is less As a multitasking operating system can carry out more
time critical and garbage collection can be accommodated than one activity concurrently by runnmg more than a single
Kelwin Nilsen in a recent article [l] claims that real time process, a process can achieve the same result by running
garbage collectors typically have larger overheads than stop more than one thread Each thread is a different stream of
and wait or generational techniques for the following reasons control inside a single process that can execute its instructions
independently from the other threads, thus a multithreaded
e in order to provide a non interfering service with the
process IS able to perform several tasks concurrently [13].
other tasks the garbage collection is divided into many
very small incremental steps, requiring frequent context Java provides the developer with the means of creating
switches; multithreaded programs providing a set of built in classes for
e iii a limited memory environment it is necessary to creating concurrent applications Java threads are a limited
recycle memory in a more aggressive way than in non package in comparison with the POSIX thread specification
real-time applications. [14], which defines the standard for thread packages which
can be implemented on any operating system (usually known
The counter-argument is that if the garbage collector is
with the name pthreads) Java threads derive from the Green
iinplemeiited as a low priority software thread into the Thread package written on top of SunOS 4.1.3, which did not
program, it is possible to achieve fast context switching
have kernel thread support They are internal threads and do
without aiiy call to the operating system kernel [121.
not take advantage of possible support of the operating system
To sum up, garbage collection offers several advantages for kernel threads Java supports threads at the language level,
over manual memory allocation, but at the same time it slows while in the majonty of the other programming languages it
down the execution speed of the program and it requires more is possible to use them only through the use of additional
memory In general this is not a problem, since many of the external libraries For this reason Java threads are well
actual platforms have plenty of computational power and integrated in the language In adchtion they are simple to use
memory On the other hand garbage collection overhead is a siiice they lack some of the advanced capabilities present in
problem for small embedded applications, where CPU speed other packages
and inemory may be a limiting factor However
Java, similar to the POSIX proposals, offers full s~ipport
e For very small embedded applications it might be for creating, starting, stopping, resuming, killing and joining
possible to preallocate all the objects and eliminate the threads
garbage collector since dynamic allocation will not be
Synchronisation is essential in a multithreaded
used. environment since it allows threads to co-ordinate their
e Miiiiinal extensions might be made to the language for activity when they try to manipulate the same data at the same
system programming and low level access to aIIow the
time (called data race) or to access a resource This is done by
programmers to develop custormsed memory-managers using fiinctional calls which limit the number of threada
(this is done by C programmers when there is no which can access the same data, or resource, at the saine time
operating system support). This may be achieved The POSIX standard defines several functions for achieving
introducing a small set of functions for accessing raw synchronisation: mutex, condition variables, semaphores,
memory and registers (as in Oberon) or explicitly readedwriters locks and barriers. Java threads do not
marking the “unsafe accesses” to the memory (as in implement such a variety of tools The main synchronisation
Modula-3). For security reasons these operations can be mechanism to control access to critical sections of the code is
allowed only in stand-alone Java applications and not in based on Hoare monitors [15] . A monitor is basically a lock
applets which can be downloaded over the network The
which is used to protect certain section of code When a
disadvantage is that this may lead to the possibility of thread enters a critical section any other thread trying to enter
writing Java programs which could contain pieces of code is forced to wait until the first thread has released the
1971

monitor. Monitors are quite similar to the concept of mutex Another advantage of Java consists in the fact that its
locks. The difference consists in the fact that a mutex lock has libraries have been designed as thread safe class libraries.
to be acquired and released explicitly while this is done This means that they can be safely used in a multithreaded
automatically in a monitor. The use of monitors avoids the environment without producing unexpected behaviour, since
problem of releasing a lock too soon or never releasing it. In the sections of the code which contain critical operations are
Java a thread can acquire a monitor by entering a method defined as synchronized.
which has been defined as synchronised. In a similar way Scheduling consists in the set of activities which allow a
access to objects can be locked using a monitor. Java does not set of tasks to be executed on a processor. In Java this consists
explicitly support condition variables, but the same behaviour in selecting a thread and running it on the JavaVM. A thread
can be obtained using the wait ( ) and notify ( ) methods. can be in several different states; runnable, non-runnable or
These can be used only from within synchronised dead. The scheduler selects among the runnable threads the
methods. When a thread calls the wait ( ) method, it blocks one to be executed. JavaVM uses a priority based scheduler in
and waits for a certain condition to be satisfied. At the same order to determine which thread is scheduled to run. This
time the monitor associated with the object is released means that higher priority threads will be scheduled for
allowing other synchronised methods to run. The notify ( ) execution before low priority ones. The scheduler is also pre-
method signals a waiting thread to wake from the wait ( ) emptable which means that if a thread whose priority is
method. The notifyAll ( ) method broadcasts the higher then the running one becomes active, it forces off the
information awakening all the waiting threads which will race processor the running thread and gains the control of the
to acquire the monitor. Since more than one thread may be CPU. The priority of a thread is set after its creation, but
competing to acquire the resource, the presence of a while the POSIX standard prescribes at least 32 levels of
synchronised method is necessary to assure the stability of the priorities Java supports only 10. The POSIX standard defines'
object. Semaphores are not explicitly defined inside the Java three thread scheduling policies: FIFO ( SCHED -FIFO),
threads package, but if required they can be easily round robin (SCHED-RR) and other (SCHED-OTHER). The
implemented in terms of mutex and condition variables. The first one is a simple priority based pre-emptive scheduler. It is
same is true also for readwrite locks and barriers. The fact called FIFO, first in first out, since the first thread in the
that some higher level synchronisation mechanisms are not queue at a given priority level will be the first one at that level
directly implemented in the Java thread package should not be to gain the access to the CPU. The thread runs until either it
a problem since code and object locking (monitor style) and voluntarily gives up the processor issuing a yield ( ) call, or
condition variables should be sufficient to solve the majority is blocked by a system call or it is pre-empted by a thread with
of cases where synchronisation is required [16]. higher priority. When the process releases the CPU the
Some problems related to the Java synchronisation scheduler examines the queue and determines which is the
mechanisms have been reported [17]. First of all the there is highest priority runnable thread which gains the CPU. The
an ambiguous timeout semantic in the wait ( ) method. Java round robin policy is similar to the FIFO one, except that an
defines three forms of wai t ( ) : active thread is automatically preempted after it has been
public final void wait() running for a quantum of time. In other words the main
throws InterruptedException; difference consists in the fact that the time is shared between
public final void wait(1onq millisecTimeout) runnable threads of the same priority. The last option is
throws InterruptedException; implementation specific. Unfortunately Java leaves certain
public final void wait(longmillisecTimeout,
int nanosecTimeout) aspects of the implementation of scheduling up to the
throws InterruptedException; individual implementations of the JavaVM. In the Java
release 1.0, Sun's Java interpreter for Windows 95 and NT
The first version of wait ( ) blocks indefinitely until is
platforms, and the Netscape Navigator Java environment used
awakened by a notify ( ) signal. The other two forms of
a round robin policy, while Sun's Java 1.0 for the Solaris
wait block only until they are notified, or until the timeout UNIX platforms does not [20]. In particular in the Java
occurs. The problem consists in the fact that it is not possible Threads Whitepaper [21] it is stated:
to know whether the wait (. . call returns because it received
a noti f y ( ) call or because a timeout occurred. It has been If the operating system supports the time slicing of
noted that Java has error prone nested monitor semantics. threads, then in most Java threads be time
Solutions to this problems are given in [lS]. There is no way However, if the Operating system does not
to spec@ which thread should be awakened by a n o t i f y ( ) suPPofl thread time slicing, then the Java threads will
method without the support of external classes [19]. Higher not be time
priority threads are not favoured over lower priority ones by The implication of this statement is that different thread
the scheduler when it is necessaq to awaken a thread blocked implementations could give different behaviours of an
in a wait after receiving a notify ( ) method. The application. It could also happen that the same
solution is to send a notifyAll ( ) method, which however implementation could produce different outcomes on different
causes a context switching overhead. runs of the application. Sun Microsystems has been criticised
1972

on this point since the decision to leave the thread scheduling Microsystems is still pursuing the application of Java in
policy to the implementation defeats the idea of producing consumer electronics, and that a certain level of real-time
platform independent applications This is even stranger if we support is likely to be included in future releases of the
consider the fact that Sun Microsystems decided to implement language.
a type of thread which does not take advantage of kernel level
threads (since not all the operating systems implement them), VI1 REFERENCES
but yet they gave complete freedom about thread scheduling [l] K. Nilsen, “Embedded real-tme development in the Java
policy. language,” in Embedded Systems Conference East, (Boston), April
Summarising, the Java thread package offers all the tools 1996
for implementing simple concurrency models in a easy way. If [2] IEEE, POSE lb, Application Program Interface - Real Time
more sophisticated synchronisation mechanisms are required Extensions, 1995 Draft Version
[3] B 0 Gallmeister, POSE 4, Programming for the Real World
some effort is needed to implement them; however, the
O‘Reilly &Associates Inc , 1995
reduction of performance due to overheads could be
[4] D Flanagan, Java in a Nutshell O’Reilly & Associates, Inc,
problematical. A good collection of solutions to common 1996
concurrency problems in Java is presented in [IS] The non- [5] K Arnold and J Gosling, The Java Programming Language
sta‘ndard scheduling policies implemented on the diEerent Addison Wesley, 1996
versions of the JavaVM present a serious problem for the use [6] T A Probstmg, P B G Townsend, J H H Newsham, and S
of the language for system independent applications and Watterson, “Toba Java for Applications, A way Ahead of Time
especially for the implementation of real-time ones In order (WAT) Compiler,” in Proceedings of the 3rd Conference on 00
to fix this problem it will be necessary to add methods, similar Technologies and Systems, 1997
to those described by the POSIX standard, to specify thread [7] J Project, “Kaffe A free virtual machine to run Java code”
available from http Ilwww kaffe org/, 1997
scheduling policies and attributes. 81 IS Nilsen, “Issues in the design and implementation of real-time
Java,” Java Developer’sJoumal, 1996
VI. CONCLUSIONS [9] J S Young, “Integration of a real-time programming system for
dynamic reactive systems in an object-onented language,” tech rep ,
Java offers several interesting features for the development
Department of EE and CS, University of California, Berkeley, 1997
of real-time applications However it cannot yet be considered available at http Ilwww-cad eecs berkeley.edu/ -jimy/java/
as the language which will resolve all the problems related to JavaRealTimeReportpdf
real-time programming. It is important here to distinguish the [ 101 P R Wilson, “Umprocessor garbage collection techniques ”

language Java from the other parts of the Java Submitted to ACM Computing Surveys, available at
implementation. As a language, Java has great advantages ftp llftp cs utexas eddpublgarbagehigsurvps, 1994
over C and C++ in terms of safety and simplicity, but it lacks [113 S Microsystems, “The Java virtual machine specification ”

their flexibility in interfacing with the hardware This means http Iljava sun coddocslvmspec A4 ps tar Z, August 1995 Release
that at present Java is more suitable for applications which do 1 0 Beta, DRAFT
[12] F Mueller, “Implementing POSE threads under UNTX
not require interaction with external devices (e.g. multimedia,
Descnption of work in progress,” in Second Sof’tware Engineering
games) The future of the Java chips is not clear, we need to Research Forum, pp 253--261, November 1992
know their exact potential and limitations. Garbage collection [13] B Lews and D J Berg, Threads Primer, A Guide to
is a problem for small applications, but the knowledge Multithreaded Programing SunSoft Press, 1996
required to implement real-time garbage collectors is [14] IEEE, POSE 1, System Application Program Interface -
available at present. The built-in support for threads allows Amendment 2 Threads Extension (C language), 1995 Also ISOllEC
simple development of concurrent applications The language 9945-1 1990c
does not have all the features which are defined in the [15] C A R Hoare, “Monitors an operating system structuring
POSIX1.b standard, but several of them can be implemented concept,” Communications of the ACM, vol 17, pp 549--557,
October 1974
at language level, and others at the level of the JavaVM. The
[IG] S Kleiman, D Shah, and B Smaalders, Programming wth
performance of the interpreted version of Java is currently Threads Prentice Hall, 1996
slow, but it is rapidly improving with the introduction of [17] P Jam and D C Schmidt, “Experiences converting a C++
native and JIT compilers. There are already in the literature communication software framework to Java,” C++ Report Magazine,
some examples of projects using Java in real-time January 1997
applications, but they are still at the development stage In [18] D. Lea, Concurrent Java Design Principles and Pattems
any case the introduction of new keywords in the language for Addison-Wesley, I996
the support of real-time features should be done by Sun [19] D Cargill, “Specific notation for Java thread synchronization,”
Microsystems in order to eliminate the problems of in Proceedings of the 3rd Pattem Languages of Programming
introducing non-standard Java dialects. Extensions to, and Conference, (Allerton Park, Illinois), September 1996
[20] P Niemeyer and J Peck, Exploring Java OReilly &
implementation of a JavaVM designed for the real-time world Associates, Inc, 1996
would be a more satisfactory alternative in order to allow Java I211 D J Berg, “Java threads A whitepaper” Available from
to achieve real-time performance The recent announcement http liwww sun codworkshoplsiglthreads/papers/JavaThreads ps,
of a version of Java for embedded systems suggests that Sun 1996

You might also like