View RSS Feed

My Java Tips

The Thread Class

Rate this Entry
by , 10-29-2011 at 07:29 PM (679 Views)
java.lang.Thread is used to create and control threads. A new instance of this class must be created to use a new thread. Once a thread is created, it does not start running right away. Thread.start() must be called to actually make the thread run. On calling Thread.start(), the thread begins executing in the run() method of the target class.

Remember that a new Thread class always starts running the public void run() method of a class. There are two ways to create a thread:

First one is to extend the Thread class. Now the new class inherits from the class Thread and the thread can start running in the class's run method. Second approach is to Implement the Runnable interface. To avoid multiple inheritance, this technique is mostly used. It is not necessary to define a new class to run the thread.

There isn't much difference between the two approaches. Both extending the Thread class and implementing the Runnable interface have the same functionality. The interface approach must be used if the thread is to actually start in the applet class. But if the thread is going to be running in its own class, it may be more convenient to extend the Thread class. Examples of both approaches are in this chapter.

The Thread class provides seven constructors which create a new thread. As stated before, the thread does not start running until Thread.start() is called. When Thread.start() is called, the new thread starts running in the run() method of an object. So define the functionality in the run() method.

The constructors are the following:

Java Code:
The constructors can use three possible parameters:

* String The name of the new thread is the parameter String. A thread can get its name by calling Thread.getName().
* ThreadGroup The new thread will belong to the group specified by the parameter ThreadGroup. A ThreadGroup can be used to organize a thread.
* Runnable The Runnable parameter is an object that has implemented the Runnable interface. The thread will start executing in the run() method of the Runnable parameter when Thread.start() has been called.

In this post, I wil list methods provided by Thread class that are used to control the thread execution.

There are many methods in the Thread class. Some of the methods that control the thread execution are the following:

- start()
This method starts the thread. It starts executing in the run() method of its Runnable target that was set when the constructor was called. This method can be called only once.

- suspend()
This method suspends the execution of the thread. It remains suspended until resume() is called.

- resume()
This method resumes the execution of a suspended thread. It has no effect on a thread that is not suspended.

- stop()
This method stops and kills a running thread. Currently, the thread does not stop unless it is running. If it is suspended, it does not die until it starts running again. However, this may be fixed someday.

- sleep(int m)/sleep(int m,int n)
The thread sleeps for m milliseconds, plus n nanoseconds.

I will present an example that shows how to start, stop, suspend, and resume threads.

It uses the Runnable interface because it extends from Applet class and multiple inheritance is not allowed. This example uses a thread that counts and prints a string every second. The thread starts when the applet is initialized. It continues to run until the user leaves the page. If the user returns to the page (and all is well), the thread continues from where it left off. This allows applets to retain their states while the user is away.

Java Code:
  public void stop()
      System.out.println("in stop() -- suspending thread.");

  public void destroy()
      System.out.println("in destroy() -- stoping thread.");

  public void run()
      int i=0;
          System.out.println("At " + i + " and counting!");
          try {myThread.sleep(1000);}
          catch (InterruptedException e ) {}

Submit "The Thread Class" to Facebook Submit "The Thread Class" to Digg Submit "The Thread Class" to Submit "The Thread Class" to StumbleUpon Submit "The Thread Class" to Google

Updated 10-29-2011 at 07:32 PM by Java Tip

Tags: None Add / Edit Tags
Java ME , Java SE