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:

  1. public class Runner implements Runnable
  2. {
  3. public void run()
  4. {
  5. while(true)
  6. {
  7. // do lots of interasting stuff
  8. // ..
  9. //Give other threads a chance
  10. try{
  11. Thread.sleep(10);
  12. }catch(InterruptedException e)
  13. {
  14. // This thread’s sleep was interrupted by another thread
  15. }
  16. }
  17. }
  18. }

 

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

 

Threads Computing in Java

 

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.

 

  1. public class Runner implements Runnable
  2. {
  3. private boolean timeToQuit = false;
  4. public void run()
  5. {
  6. while(!timeToQuit)
  7. {
  8. // do work until we are told to quit
  9. }
  10. // clean up before run() ends
  11. }
  12. public void stopRunning()
  13. {
  14. timeToQuit = true;
  15. }
  16. }

 

  1. public class ThreadController
  2. {
  3. private Runner r = new Runner();
  4. private Thread t = new Thread(r);
  5. public void startThread()
  6. {
  7. t.start();
  8. }
  9. public void stopThread()
  10. {
  11. // use specific instance of Runner
  12. r.stopRunning();
  13. }
  14. }

Within a particular piece of code , you can obtain a reference to the current thread using the static Thread method currentThread.

For example:

  1. public class NameRunner implements Runnable
  2. {
  3. public void run()
  4. {

while(true)

  1. {
  2. // lots of interasting stuff
  3. }
  4. // print name of the current thread
  5. System.out.println(“Thread ” + Thread.currentThread().getName + “ completed.”);
  6. }

}

Written by 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 Comment