Threads Computing in Java – Part 2
Starting the Thread:
- A newly created thread does not start running automatically. You must call its start method.
- For example, you can issue the following command as the previous example:
t.start();
- Calling the start() method places the virtual CPU embodied in the thread into a runnable state, meaning that it becomes viable for scheduling for execution by the JVM.
- It does not necessarily mean that thread runs immediately.
Thread Scheduling:
- Usually, in JAVA technology threads are pre-emptive, but not necessarily time-sliced (the process of giving each thread an equal amount of CPU time).
- It is a common mistake to believe that pre-emptive is another word for does time-slicing.
- The model of a pre-emptive scheduler is that many threads might be runnable, but only one thread is running.
- This thread does continue to run until it does cease to be runnable or until another thread of higher priority becomes runnable.
- In a latter case, lower priority thread is pre-empted by the thread of higher priority, which gets a chance to run insted.
- A thread might cease to be runnable (that is, become blocked) for a variety of reasons.
- The thread’s code can execute a Thread.sleep() call, asking the thread to pause deliberately for a fixed period of time.
- The thread may have to wait to access a resource and cannot continue until that resource becomes available.
- All threads that are runnable are kept in pools according to the priority. When a blocked thread does become runnable, it is placed back to the appropriate runnable pool.
- Threads from the highest priority non-empty pool are given CPU time.
A Thread object can exist in several different states throughout its lifetime.
- Although the thread becomes runnable, it does not always start running immediately. Only one action at a time is performed on a machine with one CPU.
- The following paragraphs describe how the CPU is allocated when more than one thread is runneble.
Given that JAVA threads are not necessarily time-sliced, you must ensure that the code for your threads gives other threads a chance to execute from time to time. This can be achieved by issuing the sleep call at various intervals.
For exampe:
- public class Runner implements Runnable
- {
- public void run()
- {
- while(true)
- {
- // do lots of interasting stuff
- // ..
- //Give other threads a chance
- try{
- Thread.sleep(10);
- }catch(InterruptedException e)
- {
- // This thread’s sleep was interrupted by another thread
- }
- }
- }
- }
- Above example shows how the try and catch block is used. The Thread.sleep() and other methods that can pause a thread for periods of time are interruptible.
- Threads can call another thread’s interrupt method, which signals the paused thread with an InterruptedException.
- The sleep is a static method in the Thread class, because, it does operate on a current thread and is reffered to as the Thread.sleep(x).
- The sleep method’s argument specifies the minimum number of milliseconds for which the thread must be made inactive.
- The execution of the thread does not resume until this period unless it is interrupted, in which case execution is resumed earlier.
Terminating a Thread:
- When a thread completes execution and terminates, it cannot run again.
- You can stop a thread by using a flag that indicates that the run method should exit.
- public class Runner implements Runnable
- {
- private boolean timeToQuit = false;
- public void run()
- {
- while(!timeToQuit)
- {
- // do work until we are told to quit
- }
- // clean up before run() ends
- }
- public void stopRunning()
- {
- timeToQuit = true;
- }
- }
- public class ThreadController
- {
- private Runner r = new Runner();
- private Thread t = new Thread(r);
- public void startThread()
- {
- t.start();
- }
- public void stopThread()
- {
- // use specific instance of Runner
- r.stopRunning();
- }
- }
Within a particular piece of code , you can obtain a reference to the current thread using the static Thread method currentThread.
For example:
- public class NameRunner implements Runnable
- {
- public void run()
- {
while(true)
- {
- // lots of interasting stuff
- }
- // print name of the current thread
- System.out.println(“Thread ” + Thread.currentThread().getName + “ completed.”);
- }
}