Win a copy of Re-engineering Legacy Software this week in the Refactoring forum
or Docker in Action in the Cloud/Virtualization forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

run Vs. start

 
Sudhanshu Mishra
Ranch Hand
Posts: 236
1
Eclipse IDE Fedora Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 59
Android Eclipse IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
try this code.



 
Ashwin Sridhar
Ranch Hand
Posts: 277
Flex Oracle Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Sudhanshu Mishra
Ranch Hand
Posts: 236
1
Eclipse IDE Fedora Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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?
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic