wood burning stoves 2.0*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes run Vs. start Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "run Vs. start" Watch "run Vs. start" New topic

run Vs. start

Sudhanshu Mishra
Ranch Hand

Joined: May 28, 2011
Posts: 215

Hi all,
If i have a synchronized method and two threads use the same instance to run the method ,but i run one thread by run(),and other by start().Does the synchronization of the method still prevents the threads from accessing the method at the same time?

nitin sethi
Ranch Hand

Joined: Jul 14, 2011
Posts: 59

try this code.

Ashwin Sridhar
Ranch Hand

Joined: Jul 09, 2011
Posts: 272

When you call the run() method, you are not running the code in a new thread but you are running it on your Main method thread itself. Try printing the Thread name, you will find it

Ashwin Sridhar
Sudhanshu Mishra
Ranch Hand

Joined: May 28, 2011
Posts: 215

Yes Ashwin,i do agree by what you say,
but even if the thread is using main methods call stack,but if a method is synchronized,then how come two different threads access a sync. method on a single object?
the only difference is that one method uses its run() directly,but the other uses run() via start(),so i guess both will try to acquire lock on the same object.
Is it true?
jQuery in Action, 2nd edition
subject: run Vs. start
Similar Threads
start() method, revisited after thread stops ?
multiple threads
run and start together
concurrent threads