Thread in Java

By |2015-04-25T12:09:51+05:30April 22nd, 2015|java|

Java supports concurrent programming, through its supports for Thread programming. A Thread in Java is a small part of heavy program which is executed individually. It is a sequential path of execution.

In Java a Thread is represented by the java.lang.Thread class. The JVM treats a Thread as an object of java.lang.Thread class who’s behavior is defined in the run method, thus in Java a thread is an object of the thread class.

The thread behavior is the run()  method, which is actually declared in interface called runnable interface.

Advantages of Thread:

[gmw form=”results”]

  • A Thread is light wait
  • We can do one or more tasks simultaneously. Ex: In the word document spell checker thread running internally, while we are typing.
  • Inter thread communication is less expensive when compared to inter process communication.

Thread

How to Create A Thread In Java ?

To create a thread in Java, we can follow one of the bellow approaches.

The Extinction mechanism

The Implementation mechanism

The Extension Mechanism:

In the Extension mechanism we can crate a sub class of our own, that extends the Thread class and define the Thread behavior with in the newly created class. And when ever required we can instantiate the sub class and start the Thread behavior with the start().

Example:


class MyThread  extends Thread {

    public void run() {
        -----
        -----

    }

}

Thread t = new MyThread();

t.start();

The Implementation Mechanism:

In the implementation mechanism, we can create a class which implements the runnable interface. the runnable interface is the interface which contains the declaration of the Thread behavior run().

Example:


class MyThread  implements Runnable {

    public void run() {
        -----
        -----

    }

}

Actually the above MyThread class is not a thread, because we can not acquire the all Thread class properties here. We acquire only Thread behavior called run(). And more over in this case we don’t call the run method with start() method, because start() is not available in the Runnable interface, which is actually coming from the Thread class. So that we don’t call it as a Thread. If we want to make the class as a Thread we need to follow the below syntax.


Thread t = new Thread(new MyThread());

t.start();

The above statement make the MyThread class as a Thread object. And t.start() executes the behavior as defined in the MyThread class.

The implementation mechanism is the usually considered to be bettor, as it lets us separate the Thread behavior definition from the object itself. thus giving us enhanced flexibility.

Possible constructors:

Thread() – It allocates a new Thread object.

Thread(String name) – Thread Object which will associate with the name.

Thread(Runnable target) – It allocates a new Thread object.

Thread(Runnable target,String name) – Creates a Thread object with the given name.

Thread(ThreadGroup group,String name) – we can create a thread group by using this constructor.

From the JVM point of view each program will internally converted as a Thread, from which all the child Threads are created. Those may be demon Threads or Non-demon Threads.By default all threads are non-demon threads.

By default every thread given a name, priority.

Example :


/**
 *
 * @author chandrashekhar
 */
public class MainThread {

    public static void main(String[] args) {

        Thread t = Thread.currentThread();

        System.out.println("Thread Name : " + t.getName());
        System.out.println("Thread Priority : " + t.getPriority());
        System.out.println("Thread Group : " + t.getThreadGroup().getName());
        System.out.println("Thread State : " + t.getState());
    }
}

Out put :

Thread Name : main
Thread Priority : 5
Thread Group : main
Thread State : RUNNABLE

 

In the above example MainThread class didn’t extend the Thread class and not even implement the runnable interface. But out put tells us the name, priority, group and state of the thread.

 

About the Author:

Hi Folks, you have reach this so far, that shows you like what you are learning. Then why don't you support us to improve for bettor tutorials by leaving your valuable comments and why not you keep in touch with us for latest updates on your favorite blog @ facebook , twitter , Or Google+ ,

Leave A Comment