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 run Vs. start Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of JavaScript Promises Essentials this week in the JavaScript forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "run Vs. start" Watch "run Vs. start" New topic
Author

run Vs. start

Sudhanshu Mishra
Ranch Hand

Joined: May 28, 2011
Posts: 217

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?

Thanks...
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
SCJP | SCWCD | OCA
Sudhanshu Mishra
Ranch Hand

Joined: May 28, 2011
Posts: 217

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?
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: run Vs. start