Submit Your Article
Home Articles News Tutorials Videos Add An Article
Topics: Design Photoshop Programming PHP CSS Java Database Web Development Javascript Ajax
– Close + Open

Find Out More About DevWebPro!

Sign up for the newsletter


» Terms & Conditions

Welcome to the New DevWebPro!

DevWebPro Includes:
  Hundreds Of Tutorials   Developer News
  Unique Gadget Videos   Tons of Topics to Discuss
  Expert Advice   We Will Publish Your Articles

Starting A Thread In JAVA

By: Vaibhav Pandey
Tuesday, February 2nd, 2010
Text: Decrease Font Size Increase Font Size | Print Print Article | Share: Delicious Digg StumbleUpon Post to Twitter Post to Facebook

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.

Comments

Topics: ,

About the Author:
Vaibhav Pandey got offered employment from an Indian Multinational IT Company. He is 21 years old. He has a huge interest in Java programming and has liked it from his study days. Vaibhav loves to blog and share his experiences and thoughts. He now resides in Lucknow, a state capital in India. Check out his blog at http://javatutorialsworld.blogspot.com.
DevWebPro is an iEntry Network ® publication - © 1998-2010 All Rights Reserved