Thread in JAVA : Part-1
Recently we had seen basics of threads in this article. Now we will look in detail about Threads in Java part I to Part IV.
Basic Control of Threads:
In this, it describes how to control threads.
Testing Threads:
- A thread can be in an unknown state. Use the method isAlive to determine if a thread is still viable.
- The term Alive does not imply that the thread is running; it returns true for a thread that has been started but has not completed its task.
Accessing Thread Priority:
- Use the getPriority method to determine the current priority of the thread.
- Use the setPriority method to set priority of the thread.
- The priority is an integer value. The Thread class includes the following constants:
Thread.MIN_PRIORITY
Thread.NORM_PRIORITY
Thread.MAX_PRIORITY
Putting Threads on Hold:
- Mechanisms exist that can temporarily block the execution of the thread.
- You can resume execution as if nothing happned. The thread appears to have executed an instruction very slowly.
The Thread.sleep() Method:
- The sleep method is one way to halt a thread for a period of time.
- Recall that the thread does not necessarily resume its execution at the instant that the sleep period expires.
- This is because some other thread could be executing at that instant and may not be unscheduled unless one of the following occurs:
The thread waking up is of a higher priority.
The running thread blocks for some other reason.
The join Method:
- The join method causes the current thread to wait until the thread on which the join method is called terminates.
For example:
- public static void main(String args[])
- {
- Thread t = new Thread(new Runner());
- t.start();
- …
- // do stuff in parallel with the other thread for a while
- …
- // wait here for the timer thread to finish
- try{
- t.join();
- }catch()
- {
- // t came back early
- }
- …
- // Now continue in this thread
- …
- }
- You can also call the join method with a time-out value in milliseconds.
For example:
void join(long timeout);
- For this example, the join method either suspends the current thread for timeout milliseconds or until the thread it calls on terminates.
The Thread.yield() Method:
- Use the method Thread.yield() to give other runnable threads a chance to execute.
- If other threads are runnable, yield places the calling thread into the runnable pool and allows another runnable thread to run.
- If no other threads are runnable, yield does nothing.
- A sleep call gives threads of lower priority a chance to execute. The yield method gives other runnable threads a chance to execute.
Other Ways to Create Threads:
- So far, you have seen how you can create thread context with a seperate class that implements Runnable.
- In fact, this is not the only possible approach. The Thread class implements the Runnable interface itself, so you can create a thread by creating a class that extends Thread rather than implements Runnable.
- public class MyThread extends Thread
- {
- public void run()
- {
- while(true)
- {
- // do lots of interasting stuff
- try{
- Thread.sleep(100);
- }catch(InterruptedException e)
- {
- // sleep interrupted
- }
- }
- }
- }
- public static void main(String args[])
- {
- Thread t = new MyThread();
- t.start();
- }
Selecting a Way to Create Threads:
- Given a choice of approaches to creating a thread, how can you decide between them? Each approach has its own advantages, which are described below:
The following describes the advantages of implementing Runnable:
- From an object-oriented design point of view, the Thread class is strictly an encapsulation of a virtual CPU and, such, it should be extended only when you change or extend the behavior of that CPU model.
- Because of this and the value of making the distinction between the CPU, code, and the data parts of a running thread.
- Because JAVA technology permits single inheritance only, you cannot extend any other class, such a Applet, if you extended Thread already.
- In some situations, this forces you to take the approach of implementing Runnable.
- Because there are times when you are obliged to implement Runnable, you may prefer to be consistent and always do it this way.
The advantage of extending Thread is that the code tends to be simpler.
Note: While bothe techniques are possible, you should consider very carefully why you would extend Thread. Do so only when you change or extend the behavior of a thread, not when you implement a run method.