SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6 - OCEJPAD 6
How To Ask Questions How To Answer Questions
Originally posted by san ban:
The start() method doesn't create a new thread.
Originally posted by san ban:
It puts the thread object,on which it has been invoked, to ready or runnable state.
Betty Rubble? Well, I would go with Betty... but I'd be thinking of Wilma.
Betty Rubble? Well, I would go with Betty... but I'd be thinking of Wilma.
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
Prabhat Ranjan wrote:so what is teh difference ?
Prabhat Ranjan wrote: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
Prabhat Ranjan wrote:last question difference between Threads and threads
Steve
Prabhat Ranjan wrote:
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.
Steve
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.
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.
Steve
Steve Luke wrote:
I don't want to be too contradictory but...
Prabhat Ranjan wrote:please continue the discussion and give your comments and feedback.
Steve
Prabhat Ranjan wrote:...
call the start method to run tasks parallell.
...
if we want to execute tasks sequentially then use run() method.
Steve
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();
}
}
Did you see how Paul cut 87% off of his electric heat bill with 82 watts of micro heaters? |