06.08.2013 Views

JAVA-BASED REAL-TIME PROGRAMMING

JAVA-BASED REAL-TIME PROGRAMMING

JAVA-BASED REAL-TIME PROGRAMMING

SHOW MORE
SHOW LESS

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

the run-time system (that is, the scheduler) what work should be done first,<br />

and what activities that can wait.<br />

Consider the (so far most common) case with a single processor, and compare<br />

with every-day life at home, in school, or at work. Being just one person<br />

but with many things to be done, is a situation that all readers should be<br />

familiar with. People differ in their principles of ’what to do first’; they are<br />

using different sorts of scheduling. The same applies to operating systems. In<br />

both worlds, the way of selecting ’what to do next’ is closely related to the<br />

externally experienced properties of the person/system. Doing the right thing<br />

first is often more important than doing it in a superior way.<br />

For now, we restrict the discussion to a single multi-threaded application<br />

running on one JVM. For embedded computers, that corresponds to the<br />

software running on one processor, not necessarily using a JVM since cross<br />

compilation is often more efficient but from a programming point of view that<br />

does not matter. Thus, we need some method to inform the scheduling (in<br />

the OS, RTOS, kernel, or JVM) about the preferences of our application.<br />

Expressing importance<br />

In a company you may hear things like: "This is a high priority project...", "We<br />

have to put priority on...", or "That bug has to be fixed, with the highest priority.".<br />

Likewise, the by far most common way to influence the scheduling in<br />

operating systems is via priorities. Since Java is designed to be portable, providing<br />

the ability to set priorities on threads is a natural and understandable<br />

choice.<br />

In the java.lang.Thread class there are set- and get-methods for the priority<br />

of the thread. Additionally, there are three integer constants named<br />

MIN_PRIORITY, NORM_PRIORITY, and MAX_PRIORITY, which can be use to set typical<br />

levels of importance. The actual values of these constants are not defined<br />

and the number of priority levels are not defined either in the Java specification;<br />

you can only assume that<br />

MIN_PRIORITY < NORM_PRIORITY < MAX_PRIORITY.<br />

The rest is up to each platform and Java implementation. On, for instance,<br />

Solaris the constants have values 1, 5, and 10 respectively. Thus, a higher<br />

number means a higher priority. A new thread by default gets the priority<br />

NORM_PRIORITY but that is not required in any standard. A thread created in a<br />

Applet, or the main thread of the applet, can only get priorities in the range 1<br />

to 6 as defined for that ThreadGroup (refer to the Java API documentation,<br />

thread groups are omitted here) which is to ensure that the host application<br />

running the applet (typically the web browser) will not be disabled (starved<br />

in terms introduced later) by an applet consuming all of the CPU power.<br />

The lack of a well defined meaning of priorities in Java may give the<br />

impression that they are almost useless. We should, however, be aware of:<br />

3.1. Threads<br />

49

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!