File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Thread Big Moose Saloon
  Search | Java FAQ | Recent Topics
Register / Login


Win a copy of The Mikado Method this week in the Agile and other Processes forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Reply Bookmark "Thread" Watch "Thread" New topic
Author

Thread

Shaun Zeng
Greenhorn

Joined: Jan 17, 2008
Posts: 3
What is the differences between .run() & .start() in threading?

the only difference to me is that .run() runs straight away. Is that right ?



few.beer.wana.cum.ova?
abhrodip paul
Greenhorn

Joined: Oct 16, 2007
Posts: 27
There is some very small but important difference between using start() and run() methods.

Example one:


Thread one = new Thread();
Thread two = new Thread();
one.run();
two.run();

Example two:


Thread one = new Thread();
Thread two = new Thread();
one.start();
two.start();

The result of running examples will be different.

In Example one the threads will run sequentially: first, thread number one runs, when it exits the thread number two starts.

In Example two both threads start and run simultaneously.

because the start() method call run() method asynchronously (does not wait for any result, just fire up an action), while we run run() method synchronously - wait when it quits and only then can run the next line of our code.
Ian Edwards
Ranch Hand

Joined: Aug 14, 2006
Posts: 107
You can indeed call the run() method like any other method - but the intended use from the point of threading is that you call the start() method to start the thread and then the thread will execute your run() method.

When you call the run() method directly it is not running in a new thread.
Ulf Dittmer
Marshal

Joined: Mar 22, 2005
Posts: 35241
    
    7
Welcome to JavaRanch.

The important difference is that only calling start() will actually cause the code to run in a new thread. Calling run() will cause the code in to be run the current thread (meaning no new thread is started, thereby defeating the purpose of using Thread in the first place).


Android appsImageJ pluginsJava web charts
Shaun Zeng
Greenhorn

Joined: Jan 17, 2008
Posts: 3
Thanks all of you. really helpful.

This question takes me whole nyte.
 
I agree. Here's the link: http://ej-technologies/jprofiler - if it wasn't for jprofiler, we would need to run our stuff on 16 servers instead of 3.
 
subject: Thread
 
Similar Threads
Song Titles
It's just a bunny.
Java.exe error
Map in need of reconfiguration?
WA #1.....word association