File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Threads and Synchronization and the fly likes Difference between run and start method of Thread Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Threads and Synchronization
Bookmark "Difference between run and start method of Thread" Watch "Difference between run and start method of Thread" New topic
Author

Difference between run and start method of Thread

Anilkumar Ravinuthala
Greenhorn

Joined: Jan 17, 2007
Posts: 17
I want to know the difference between the Run method and start method of thread.
please anyone explain theoretical difference between these two.
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19670
    
  18

The start() method creates a new thread, that executes the run() method.
The run() method just executes in the current thread, without starting a new thread.


SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
How To Ask Questions How To Answer Questions
Ajay Saxena
Ranch Hand

Joined: Nov 13, 2006
Posts: 154
The start() method doesn't create a new Thread. It puts the Thread object,on which it has been invoked, to ready or runnable state.It however creates an OS thread ,say T_OS.

The run() method is best left to the JVM(the thread scheduler of JVM)for invocation.

In other words, the client code instantiating a thread shouldn't place a call to the run() method on the newly instantiated thread.Because the very fact that calling the run() method on a thread object would immediately execute the steps in the run() method defeats the very purpose of multithreaded programming which is essentially of a non-deterministic nature in terms of order of execution of the concurrently running threads.

Also calling the run() method on the newly created Thread (Java Thread object)would block the execution of the parent thread till it completes.So that makes the intended parallel processing through two threads ( the new thread and the parent thread) actually serial.

[ November 15, 2007: Message edited by: san ban ]

[ November 15, 2007: Message edited by: san ban ]
[ November 17, 2007: Message edited by: san ban ]
Peter Chase
Ranch Hand

Joined: Oct 30, 2001
Posts: 1970
Originally posted by san ban:
The start() method doesn't create a new thread.


Generally, yes it does, where "thread" means an operating system thread. It is strictly a JVM implementation detail whether starting a Java Thread starts an operating system thread, but in common JVMs, it certainly does.

Originally posted by san ban:
It puts the thread object,on which it has been invoked, to ready or runnable state.


There's no such thing as a "thread object". It's a Thread object.

If you're suggesting that the JVM creates an operating system thread in a paused state, when constructing a Thread object, then that's a JVM implementation detail. I would guess it typically does not do so. I guess that constructing a Thread object just constructs the Java object, not the underlying operating system thread.
[ November 15, 2007: Message edited by: Peter Chase ]

Betty Rubble? Well, I would go with Betty... but I'd be thinking of Wilma.
Ajay Saxena
Ranch Hand

Joined: Nov 13, 2006
Posts: 154
The start() method doesn't create a new thread
Here by thread, I actually meant a java Thread object,not an OS thread.


There's no such thing as a "thread object". It's a Thread object.

Well, by thread object , I actually meant a Thread object.Again not an OS thread. Sorry for the smallercase 't'.
Peter Chase
Ranch Hand

Joined: Oct 30, 2001
Posts: 1970
By the way, the reason I was very pedantic about your post was because you were "correcting" a previous post, but in fact the previous post was correct and yours was at best confusing and at worst wrong. I've done that, too, on occasions; we should all be extra-careful when we post "corrections".
[ November 15, 2007: Message edited by: Peter Chase ]
Stan James
(instanceof Sidekick)
Ranch Hand

Joined: Jan 29, 2003
Posts: 8791
I just want to cover the same ground again to be sure ... I found "triggering execution" potentially confusing ...

run() is a perfectly ordinary method. You wrote it. It does nothing that you didn't write. If you call run() on thread T1 it will run on T1 and return after it's done like any other method.

start() does some deep magic that we cannot do in normal Java code. Via native calls it creates a new thread (little t, assume an OS thread) and causes the new thread to call the run() method. If you call start() on T1, the JVM will create new thread T2 and call run() on T2.
[ November 16, 2007: Message edited by: Stan James ]

A good question is never answered. It is not a bolt to be tightened into place but a seed to be planted and to bear more seed toward the hope of greening the landscape of the idea. John Ciardi
Ajay Saxena
Ranch Hand

Joined: Nov 13, 2006
Posts: 154
I appreciate the criticisms on my post and regret having caused any confusions. I have edited my post with some corrections.
[ November 17, 2007: Message edited by: san ban ]
Prabhat Ranjan
Ranch Hand

Joined: Oct 04, 2006
Posts: 391
So What is the final conclusion or sumarrized.

i saw someone has posted correctly then some else critisism over that.

So i say, Start creates a new Thread T1 and call the run() method for execution of the thread T1

while run() method if it is called on Thread T1 , its just called on Thread T1.

So what is the difference and what we lost and correct way.
Ernest Friedman-Hill
author and iconoclast
Marshal

Joined: Jul 08, 2003
Posts: 24183
    
  34

We're using the word "Thread", with a capital "T", to refer to the java.lang.Thread class. We're using "thread", with a lower-case "t", to refer to an operating-system thread. The "start()" method creates a new thread; it does not create a new Thread.

Get it?


[Jess in Action][AskingGoodQuestions]
Prabhat Ranjan
Ranch Hand

Joined: Oct 04, 2006
Posts: 391
so what is the difference ?
Ernest Friedman-Hill
author and iconoclast
Marshal

Joined: Jul 08, 2003
Posts: 24183
    
  34

Prabhat Ranjan wrote:so what is teh difference ?


One is a Java object; one is a data structure of some kind created by the operating system. The Java Thread is the same on all platforms; the operating system thread is quite different (Windows has its own thread library; Linux has POSIX threads, etc.) The Java Thread object serves as a kind of standard interface to the OS threads.
Prabhat Ranjan
Ranch Hand

Joined: Oct 04, 2006
Posts: 391
No , i meant to say what is the difference between start() and run() method
Sai Hegde
security forum advocate
Ranch Hand

Joined: Oct 26, 2010
Posts: 199
    
    1

Snippet 1 --->


Snippet 2 --->


While in the first snippet, you can be assured that t1 will always run before t2, its not the case in snippet 2.
Using start() is like lighting up two firecrackers... You cannot predict which one will go off first.
Stephan van Hulst
Bartender

Joined: Sep 20, 2010
Posts: 3611
    
  14

Sai Hegde
security forum advocate
Ranch Hand

Joined: Oct 26, 2010
Posts: 199
    
    1

nice stephan
Prabhat Ranjan
Ranch Hand

Joined: Oct 04, 2006
Posts: 391
that's fine.

so what we can summarized up here.

when to use start() & run(). and why interviewers are so much of curiosity over run() & start()
Steve Luke
Bartender

Joined: Jan 28, 2003
Posts: 4174
    
  21

Prabhat Ranjan wrote:that's fine.

so what we can summarized up here.

when to use start() & run()


Use start() to have the task run in parallel to the current task, and other tasks.

Don't use a Thread object's run() method. If you want to run the task serially in the current thread then don't use a Thread object at all, instead use a Runnable or any other normal method.

. and why interviewers are so much of curiosity over run() & start()

Because the proper use of Threads and threads is important. And understanding the difference between run() and start() is one of the most basic building blocks. You have to be able to know when things are meant to run in parallel (or 'concurrently') and when they are meant to run serially (or in the same thread).


Steve
Prabhat Ranjan
Ranch Hand

Joined: Oct 04, 2006
Posts: 391
last question difference between Threads and threads
Steve Luke
Bartender

Joined: Jan 28, 2003
Posts: 4174
    
  21

Prabhat Ranjan wrote:last question difference between Threads and threads


There is a distinction between the Java Thread Object (Thread with a capital T) and the operating system thread (lowercase t). The operating system thread is what allows tasks to run in parallel - it is this operating system thread that gets created when you use the start() method. And this operating system thread is the 'stream of execution' in which the run() method eventually gets executed after calling the start() method.

The distinction comes because the Java Thread Object's constructor creates a Java Thread Object but not an OS level thread - and the start() method creates an OS level thread but not a Java Thread Object. So you have to remember that just because you have a Java Thread Object - that doesn't mean you have an OS level thread.
Prabhat Ranjan
Ranch Hand

Joined: Oct 04, 2006
Posts: 391
really impressed now understood

thanks
Prabhat Ranjan
Ranch Hand

Joined: Oct 04, 2006
Posts: 391

while



I have 2 query with the above case.

1) calling run() multiple times allowed in Runnable while t.start() multiple is not allowed. will get IllegalStateException
2) if we are calling th.run() means its not creating the OS level thread. so which thread its executing into the JVM.

please clarify.
Steve Luke
Bartender

Joined: Jan 28, 2003
Posts: 4174
    
  21

Prabhat Ranjan wrote:
1) calling run() multiple times allowed in Runnable while t.start() multiple is not allowed. will get IllegalStateException


Correct. The start() method is not allowed to be called twice. This is defined in the API: http://download.oracle.com/javase/6/docs/api/java/lang/Thread.html#start(). There is no such rule for the run() method.

2) if we are calling th.run() means its not creating the OS level thread. so which thread its executing into the JVM.

The 'current thread.' The same one used for executing surrounding code. In the above sample, there will be a new process for the JVM created and there will be a 'main' thread which begins executing the main() method for the application. When you call run() the run() code gets executed in this main thread.
Sai Hegde
security forum advocate
Ranch Hand

Joined: Oct 26, 2010
Posts: 199
    
    1

You cannot start a thread more than once. Its life-cycle is complete once it completes execution.
Use the Executors interface from the java.util.concurrent package if you had to reuse threads. The thread creation overhead is also dealt with this way.
Stephan van Hulst
Bartender

Joined: Sep 20, 2010
Posts: 3611
    
  14

You are still confused about what run() does, methinks.

run() isn't a special method. It works just like any other method you declare. If you call it, the main thread (actually, the thread you call it from) will run the code in the run method.

If you call run() twice, you simply execute the code twice. Not parallel, but sequentially.

Thread's start() has a completely different purpose. start() gets a new OS thread and lets it execute the code in the Thread's (overridden) run() method, or if you construct a Thread with a Runnable as the argument, it will let the thread execute the code in that Runnable's run() method.

A Thread models a single thread over time. When it's done running code, it's done. You can't start it again.

Note that in your second example, you are not running two threads parallel. You are simply executing the run method twice, in sequence.

Also note that in your second example, you are not calling the run method declared in your thd class. You are calling the run() method of the vanilla Thread instance, which does nothing.
Stephan van Hulst
Bartender

Joined: Sep 20, 2010
Posts: 3611
    
  14

Oh another thing. I don't want to overwhelm you, but I think it's good to get used to this early:

Don't extend Thread. You can, and it will produce perfectly legal and working code, but I consider it bad practice.

Always implement Runnable, and pass it to a vanilla Thread instance. Get used to this idiom:
Steve Luke
Bartender

Joined: Jan 28, 2003
Posts: 4174
    
  21

Stephan van Hulst wrote:Also note that in your second example, you are not calling the run method declared in your thd class. You are calling the run() method of the vanilla Thread instance, which does nothing.


I don't want to be too contradictory but...

Prabhat is passing an instance of thd (a Runnable) to the Thread instance when he is constructing it. The Thread run() method will execute the code in the Runnable's run() method. As per the API:

run()
If this thread was constructed using a separate Runnable run object, then that Runnable object's run method is called; otherwise, this method does nothing and returns.

So the vanilla Thread run() method only does nothing if no Runnable was passed to the Thread's constructor.
Prabhat Ranjan
Ranch Hand

Joined: Oct 04, 2006
Posts: 391


see here , when we create Thread using Thread class and calling
t.start();

then its not calling the run() method of runnable interface , right ?

you mean to say that its calling the run() method on 'stream of execution' process to execute(give a chance to CPU) the OS thread created by start.

am i right ?

Prabhat Ranjan
Ranch Hand

Joined: Oct 04, 2006
Posts: 391
i agree with Stephan van Hulst .

better to create thread either by Runnable or Thread
call the start method to run tasks parallell.



here no need to call run() method.Directly calling run method does nothing.

if we want to execute tasks sequentially then use run() method. which does nothing just executes the method whatever no of times we want to execute it.



Stephan van Hulst
Bartender

Joined: Sep 20, 2010
Posts: 3611
    
  14

Steve Luke wrote:
I don't want to be too contradictory but...

Woops! My bad.
Prabhat Ranjan
Ranch Hand

Joined: Oct 04, 2006
Posts: 391
please continue the discussion and give your comments and feedback.

Steve Luke
Bartender

Joined: Jan 28, 2003
Posts: 4174
    
  21

Prabhat Ranjan wrote:please continue the discussion and give your comments and feedback.



I am not sure there is anything left. Was there a particular question you still had?
Prabhat Ranjan
Ranch Hand

Joined: Oct 04, 2006
Posts: 391
Ya its fine and i just want to reply on my last 2 last 2 post..am i understood correct or not.
Steve Luke
Bartender

Joined: Jan 28, 2003
Posts: 4174
    
  21

Prabhat Ranjan wrote:...
call the start method to run tasks parallell.
...
if we want to execute tasks sequentially then use run() method.


That is correct.
Prabhat Ranjan
Ranch Hand

Joined: Oct 04, 2006
Posts: 391
thanks everyone for your view.

earlier i was really confused about run() and start() method.
S Kanak
Greenhorn

Joined: May 21, 2013
Posts: 1
I am writing in response to the following comment.

You are still confused about what run() does, methinks.

run() isn't a special method. It works just like any other method you declare. If you call it, the main thread (actually, the thread you call it from) will run the code in the run method.

If you call run() twice, you simply execute the code twice. Not parallel, but sequentially.

Thread's start() has a completely different purpose. start() gets a new OS thread and lets it execute the code in the Thread's (overridden) run() method, or if you construct a Thread with a Runnable as the argument, it will let the thread execute the code in that Runnable's run() method.

A Thread models a single thread over time. When it's done running code, it's done. You can't start it again.

Note that in your second example, you are not running two threads parallel. You are simply executing the run method twice, in sequence.

Also note that in your second example, you are not calling the run method declared in your thd class. You are calling the run() method of the vanilla Thread instance, which does nothing.


The code is as below


public class thd implements Runnable {

public void run(){
System.out.println("Run called");
}
public static void main(String args[]){

Thread th = new Thread(new thd());
th.run();
th.run();
}
}


My question:


The second one will run and print "Run called" 2 times since we are creating the Thread using thd thread as a constructor argument.

Thread th = new Thread(new Thd());

not like

Thread th = new Thread("thread");


Am I correct?
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Difference between run and start method of Thread