Friday, December 4, 2009

How to Start a Thread in Java

There are two ways of Thread creation either by implementing Runnable or extending the Thread class.In both the cases you need to start the thread for getting the functionality you need.So in order to start a thread we need a method which is start() and it is invoked on Thread object.

Remember:-start() method is always invoked on Thread object to start a separate call stack of this thread,but if you call the run() method on your Runnable object then it is simply a method call and cannot initiate a separate call stack.It also clears that You start a Thread not a Runnable.

To start a new Thread in separate call stack use

t.start() // where t is Thread object which is in execution.

We have made a call to start() method but know what happens behind the scenes there are three things happen:-

  1. A new Thread in execution starts(in a new call stack).
  2. The Thread moves from the new state to the runnable state.
  3. Whenever the Thread gets a chance to execute then it goes to running state and the target run() method will run.

Consider the following example to understand how Threads are started and also you will come to know that invoking run() method do works but it never ever initiate a new Thread or a separate call stack.

class TestRunnable implements Runnable
{
public void run()
{
for(int i=0;i<5;i++)
System.out.println("In Runnable");
}

public static void main(String a[])
{
Thread tr = Thread.currentThread();
System.out.println(tr);
TestRunnable r = new TestRunnable();
Thread t = new Thread(r);
System.out.println(r);
r.run();
System.out.println(t);
t.start();
}
}


OutPut:-

Thread[main,5,main]
TestRunnable@3e25a5
In Runnable
In Runnable
Thread[First Thread,5,main]
In Runnable
In Runnable

Explanations:-

Here in above example there are two things we wanted to get you know that new Threads are started by calling start() method on Thread object and Runnable can never ever start a new call stack.invoking run() method simply works as another method invocation.
Also you can see that when we tried to print the names of Threads then r comes with only as object names whereas the Thread t and tr have original Thread names.

Suggested Reading:-

Thread Life Cycle
Java Thread Basics
Ways of creating a Thread
Main Thread creation and handling

0 comments:

About This Blog

This Blog is all about Java and programming.This blog is written by Vaibhav Pandey(Read More about him) .He is a Computer Science Graduate .You can contact the author at javatute@gmail.com for any suggestion or Query.You can also contact the author for advertising on this blog.All the material presented here is the property of author and its reproduction in any form is strictly prohibited.

Disclaimer:-Download links provided here are not of author in any means.These are found over the Internet.
Page copy protected against web site content infringement by Copyscape

  © Blogger templates The Professional Template by Ourblogtemplates.com 2008

Back to TOP