JAVA-BASED REAL-TIME PROGRAMMING
JAVA-BASED REAL-TIME PROGRAMMING
JAVA-BASED REAL-TIME PROGRAMMING
You also want an ePaper? Increase the reach of your titles
YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.
3.1.1 Thread creation<br />
At start of a Java Virtual Machine (JVM), the so called main thread starts<br />
executing the Java program by calling the main method of the class given to<br />
the JVM. In case of an applet, the same applies but then the main method is<br />
part of the host application, which in turn calls the applet’s init method. To<br />
have another thread running, a system call has to be done (within the JVM<br />
or to the OS, at this stage we don’t care which).<br />
In most programming languages there is no built-in support for concurrency,<br />
and creating new threads has to be done via a platform and language<br />
specific system/ library call. In the Java platform, we have to create an object<br />
of type java.lang.Thread and call its method start (which is native, i.e., not<br />
written in Java). The caller of start returns as for any method call, but there<br />
is also a new thread competing for CPU time. The newly created thread must<br />
have an entry point where it can start its execution. For this purpose there<br />
must be a runnable object available to the thread object. A runnable object<br />
provides a method run according to the interface java.lang.Runnable; that interface<br />
simply declares a public void run() and nothing more. It is that run<br />
method you should implement to accomplish the concurrent activity.<br />
What run method to be called by start is determined at thread-object<br />
creation time, i.e., when the thread object constructor is run. There are two<br />
alternatives depending on what constructor that is used, extending a thread<br />
object or implementing a runnable object:<br />
• By extending class Thread, Runnable will be implemented since public<br />
class Thread extends Object implements Runnable. In other words, this<br />
will be an instance of Runnable. The default constructor will select<br />
this.run as the beginning for the new thread.<br />
Advantages with this alternative is that you can override also the start<br />
method (to do some things before/after thread creation/termination,<br />
calling super.start in between), utility methods of class Thread can be<br />
called without qualification and asking the run-time system for the currently<br />
executing thread (described in the sequel), and the inheritance<br />
is a good basis for developing further threading subclasses (as done in<br />
later chapters).<br />
• By implementing the Runnable interface, the object can be passed to the<br />
Thread constructor which then will use the run of the supplied object<br />
as the beginning for the new thread. The advantage with this approach<br />
is that the (in Java single) inheritance can be used for other purposes,<br />
such as inheriting graphical properties.<br />
In short, as indicated in the second item, which alternative that is most suitable<br />
depends on the primary issue of the software. For instance, a graphical<br />
3.1. Threads<br />
43