• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Difference between run and start method of Thread

 
Greenhorn
Posts: 17
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I want to know the difference between the Run method and start method of thread.
please anyone explain theoretical difference between these two.
 
Sheriff
Posts: 22783
131
Eclipse IDE Spring VI Editor Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Ranch Hand
Posts: 154
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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 ]
 
Ranch Hand
Posts: 1970
1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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 ]
 
Ajay Saxena
Ranch Hand
Posts: 154
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 1970
1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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 ]
 
(instanceof Sidekick)
Posts: 8791
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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 ]
 
Ajay Saxena
Ranch Hand
Posts: 154
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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 ]
 
Ranch Hand
Posts: 397
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
author and iconoclast
Posts: 24207
46
Mac OS X Eclipse IDE Chrome
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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?
 
Prabhat Ranjan
Ranch Hand
Posts: 397
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
so what is the difference ?
 
Ernest Friedman-Hill
author and iconoclast
Posts: 24207
46
Mac OS X Eclipse IDE Chrome
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Posts: 397
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
No , i meant to say what is the difference between start() and run() method
 
security forum advocate
Posts: 236
1
Android Flex Google App Engine
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Saloon Keeper
Posts: 15510
363
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
 
Sai Hegde
security forum advocate
Posts: 236
1
Android Flex Google App Engine
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
nice stephan
 
Prabhat Ranjan
Ranch Hand
Posts: 397
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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()
 
Bartender
Posts: 4179
22
IntelliJ IDE Python Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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).
 
Prabhat Ranjan
Ranch Hand
Posts: 397
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
last question difference between Threads and threads
 
Steve Luke
Bartender
Posts: 4179
22
IntelliJ IDE Python Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Posts: 397
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
really impressed now understood

thanks
 
Prabhat Ranjan
Ranch Hand
Posts: 397
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Posts: 4179
22
IntelliJ IDE Python Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Posts: 236
1
Android Flex Google App Engine
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Saloon Keeper
Posts: 15510
363
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Saloon Keeper
Posts: 15510
363
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 4179
22
IntelliJ IDE Python Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Posts: 397
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator


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
Posts: 397
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Saloon Keeper
Posts: 15510
363
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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


Woops! My bad.
 
Prabhat Ranjan
Ranch Hand
Posts: 397
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
please continue the discussion and give your comments and feedback.

 
Steve Luke
Bartender
Posts: 4179
22
IntelliJ IDE Python Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Posts: 397
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 4179
22
IntelliJ IDE Python Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Posts: 397
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
thanks everyone for your view.

earlier i was really confused about run() and start() method.
 
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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?
 
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thread.start() method(native method) of Thread class actually does the job of running the Thread.run() method in a thread.
If we directly call Thread.run() method it will executed in the same thread,so does not solve the purpose of creating new thread.

Source:- Difference between Thread.start() & Thread.run() method
 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic