2. Thread Attributes

2.1. Thread class

It implements a system independent definition of Java threads.

2.2. Thread Body: The run() method

After a thread has been created and initialized, the runtime system calls its run() method.

  1. Subclassing the Thread class defined in the java.lang package and override the run() method.

  2. Providing a class that implements the Runnable interface and giving to a new thread a handle to an instance of it.

When to use each?

If your class must subclass some other class, you should use Runnable as described in option #2.

Audio in Portuguese

Figure 7.1. Thread States

Thread States

2.3.1. New Thread

Thread myThread = new MyThreadClass();

Definition: It is merely an empty Thread object. No system resources have been allocated for it yet.

2.3.2. Runnable

Thread myThread = new MyThreadClass();
myThread.start();

Definition: It is when the start() method creates the system resources necessary to run the thread and schedules the thread to run.

[Note]Note

The thread might not actually be running when it is in this state.

Audio in Portuguese

2.3.3. Not Runnable

Definition: A thread enters the "Not Runnable" state when one of these four events occurs:

  • Someone invokes its sleep() method.

  • Someone invokes its suspend() method.

  • The thread uses its wait() method to wait on a condition variable.

  • The thread is blocking on I/O.

[Note]Note

Even if the processor becomes available a "Not Runnable" thread does not run.

Leaving the "Not Runnable" state:

  • If a thread has been put to sleep, then the specified number of milliseconds must elapse.

  • If a thread has been suspended, then someone must call its resume() method.

  • If a thread is waiting on a condition variable, whatever object owns the variable must relinquish it by calling either notify() or notifyAll().

  • If a thread is blocked on I/O, then the I/O must complete.

2.3.4. Dead

From natural causes:

A thread dies naturally when its run() method exits normally.

Being killed:

A thread is killed when its stop() method is called.

Thread myThread = new MyThreadClass();
myThread.start();
try {
    Thread.currentThread().sleep(10000);
} catch (InterruptedException e){
}
myThread.stop();

The stop() method throws a ThreadDeath object at the thread to kill it.

Audio in Portuguese

Scheduling: Execution of multiple threads on a single CPU, in some order.

Priority: The Java supports a very simple, deterministic scheduling algorithm known as fixed priority scheduling.

When a Java thread is created, it inherits its priority from the thread that created it.

Priority Modification: setPriority() method. Thread priorities are integers ranging between MIN_PRIORITY and MAX_PRIORITY.

Preemptive Scheduling: If a thread with a higher priority than the currently executing thread needs to execute, the higher priority thread is immediately scheduled.

Equal Priority Scheduling:

The scheduler chooses in a round-robin fashion. The chosen thread will run until one of the following conditions is true:

  • a higher priority thread becomes "Runnable"

  • it yields, or its run() method exits

  • on systems that support time-slicing, its time allotment has expired

Then the second thread is given a chance to run, and so on, until the interpreter exits.

[Note]Note

Use priority only to affect scheduling policy for efficiency purposes. Do not rely on thread priority for algorithm correctness.

Time-Slicing:

Time-sliced system divides the CPU into time slots and iteratively gives each of the equal-and-highest priority threads a time slot in which to run.

[Note]Note

The Java runtime does not implement (and therefore does not guarantee) time-slicing.

  • Daemon threads are those that provide a service for other threads in the system.

  • When the only remaining threads in a process are daemon threads, the interpreter exits.

Audio in Portuguese