Introduction to Java threads

A quick tutorial on how to implement threads in Java

1 2 Page 2
Page 2 of 2

Scheduling

Java has a Thread Scheduler that monitors all running threads in all programs and decides which threads should be running and which are in line to be executed. There are two characteristics of a thread that the scheduler identifies in its decision process. One, the most important, is the priority of the thread, the other is the daemon flag. The scheduler's basic rule is if there are only daemon threads running, the Java Virtual Machine (JVM) will exit. New threads inherit the priority and daemon flag from the thread that created it. The scheduler determines which thread should be executed by analyzing the priorities of all threads. Those with the highest priority are allowed execution before any lower priority threads.

The scheduler can be of two flavors, preemptive or non-preemptive. Preemptive schedulers give a certain time-slice to all threads running on the system. The scheduler decides which thread is next to run and resume() that thread for some constant period of time. When the thread has executed for that time period it will be suspended() and the next thread scheduled will be resumed(). Non-preemptive schedulers decide which thread should run and run it until the thread is complete. The thread has full control of the system for as long as it likes. The yield() method is a way for a thread to force the scheduler to start executing another waiting thread. Depending on the system Java is running on, the scheduler can be either preemptive or non-preemptive.

Priorities

The scheduler determines which thread should be running based on a priority number assigned to each thread. The range of priorities is from 1 to 10. The default priority of a thread is Thread.NORM_PRIORITY, which is assigned the value of 5. Two other static variables are made available, they are Thread.MIN_PRIORITY, which is set to 1, and Thread.MAX_PRIORITY, which is set to 10. The getPriority() method can be used to find the current value of the priority of a thread.

Daemon threads

Daemon threads are sometimes called "service" threads that normally run at a low priority and provide a basic service to a program or programs when activity on a machine is reduced. An example of a daemon thread that is continuously running is the garbage collector thread. This thread, provided by the JVM, will scan programs for variables that will never be accessed again and free up their resources back to the system. A thread can set the daemon flag by passing a true boolean value to the setDaemon() method. If a false boolean value is passed, the thread will become a user thread. However, this must occur before the thread has been started.

Scheduling example

The following applet demonstrates the execution of two threads with different priorities. One thread is running at the lowest priority and the other at the highest priority. The threads will count until the faster thread's counter catches up to the slower threads counter.

ScheduleThreads Example and source code

Conclusion

Using threads in Java will enable greater flexibility to programmers looking for that extra edge in their programs. The simplicity of creating, configuring and running threads lets Java programmers devise portable and powerful applets/applications that cannot be made in other third-generation languages. Threads allow any program to perform multiple tasks at once. In an Internet-aware language such as Java, this is a very important tool.

Donald G. Drake has been programming in Java since the alpha release. Drake has written applets designed to spice up web sites. The most popular is his version of a TickerTape (http://www.netobjective.com/java/TickerTapeInfo.html), which can be extensively configured to blend into any web page. Drake's background includes a Bachelor of Science degree in Computer Science from John Carroll University. He is currently pursuing a Masters of Science Degree at DePaul University.

Learn more about this topic

1 2 Page 2
Page 2 of 2