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.

 

Threads in Java : Part I

 

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:

  1. public static void main(String args[])
  2. {
  3. Thread t = new Thread(new Runner());
  4. t.start();
  5. // do stuff in parallel with the other thread for a while
  6. // wait here for the timer thread to finish
  7. try{
  8. t.join();
  9. }catch()
  10. {
  11. // t came back early
  12. }
  13. // Now continue in this thread
  14. }

 

  • 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.

 

  1. public class MyThread extends Thread
  2. {
  3. public void run()
  4. {
  5. while(true)
  6. {
  7. // do lots of interasting stuff
  8. try{
  9. Thread.sleep(100);
  10. }catch(InterruptedException e)
  11. {
  12. // sleep interrupted
  13. }
  14. }
  15. }
  16. }
  17. public static void main(String args[])
  18. {
  19. Thread t = new MyThread();
  20. t.start();
  21. }

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.

Sourabh Bhunje

Sourabh Bhunje, B.E. IT from Pune University. Currently Working at Techliebe. Professional Skills: Programming - Software & Mobile, Web & Graphic Design, Localization, Content Writing, Sub-Titling etc. http://techliebe.com/about-us

Leave a Reply