Concurrent Programming with Java
Lab Manual, Version 1.0, F. Astha Ekadiyanto, 2002.

[Contents] [Next] [Previous]

Lab 4: Introduction to Threads


Some Basics about Threads

These trail has nothing to do with coding the Case Problem. It is just placed here to build a common sense about threads.
To get immediately down to business, you may go directly to defining a scheduled thread.

What is a Thread ?

Definition: A thread is a single sequential flow of control within a program.

Multithreading is when multiple threads execute byte-code instruction sequences in the same program. The benefits of multithreading are:

The Java Virtual Machine allows an application to have multiple threads of execution running concurrently.

How does it works?

Every thread has a priority. Threads with higher priority are executed in preference to threads with lower priority. Each thread may or may not also be marked as a daemon. When code running in some thread creates a new Thread object, the new thread has its priority initially set equal to the priority of the creating thread, and is a daemon thread if and only if the creating thread is a daemon.

When a Java Virtual Machine starts up, there is usually a single non-daemon thread (which typically calls the method named main of some designated class).
The Java Virtual Machine continues to execute threads until either of the following occurs:

Creating new threads for execution.

There are two ways to create a new thread of execution. One is to declare a class to be a subclass of Thread. This subclass should override the run method of class Thread. An instance of the subclass can then be allocated and started. For example, a thread that computes primes larger than a stated value could be written as follows:

class PrimeThread extends Thread {
   long minPrime;
   PrimeThread(long minPrime) {
       this.minPrime = minPrime;
   }
   
   public void run() {
       // compute primes larger than minPrime
       . . .
   }
}

The following code would then create a thread and start it running:

PrimeThread p = new PrimeThread(143);
p.start();

The other way to create a thread is to declare a class that implements the Runnable interface. That class then implements the run method. An instance of the class can then be allocated, passed as an argument when creating Thread, and started. The same example in this other style looks like the following:

class PrimeRun implements Runnable {
    long minPrime;
    PrimeRun(long minPrime) {
        this.minPrime = minPrime;
    }
   
    public void run() {
        // compute primes larger than minPrime
        . . .
    }
}

The following code would then create a thread and start it running:

PrimeRun p = new PrimeRun(143);
new Thread(p).start();

Every thread has a name for identification purposes. More than one thread may have the same name. If a name is not specified when a thread is created, a new name is generated for it.

More Readings?

If you want to know more about threads, there is at least one good tutorial about it in the Java Tutorial : Threads: Doing Two or More Tasks at Once.

The truth about this Lab?

This Lab is actually not the real Lab about Threads in Java. What we will do in this Lab is actually just using a ready to use Object that manages a Thread and handles it no more that accessing its schedule() method. Thus, we will not learn about managing threads at the moment.

But nevertheless, we are defining a simple and safe ThreadObject called TimerTask. The TimerTask subClass will be used to define scheduled tasks for future execution in a background thread through a Timer Object. Tasks may be scheduled for one-time execution, or for repeated execution at regular intervals. Since we use the background thread to animate MobileStation's movement, we will use the Timer Object's scheduler for a repeated execution of the TimerTask.


[Contents] [Next] [Previous]