After a thread has been created and initialized, the runtime system calls its run() method.
Subclassing the Thread class defined in the java.lang package and override the run() method.
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.
Thread myThread = new MyThreadClass();
Definition: It is merely an empty Thread object. No system resources have been allocated for it yet.
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 |
---|---|
The thread might not actually be running when it is in this state. |
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 |
---|---|
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.
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.
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 |
---|---|
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 |
---|---|
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.
Copyright © 1998-2009 Dilvan Moreira