This week's book giveaway is in the Servlets forum.
We're giving away four copies of Murach's Java Servlets and JSP and have Joel Murach on-line!
See this thread for details.
The moose likes Threads and Synchronization and the fly likes Concurrent execution 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 » Java » Threads and Synchronization
Bookmark "Concurrent execution" Watch "Concurrent execution" New topic
Author

Concurrent execution

Madhu Sudhana
Ranch Hand

Joined: Apr 16, 2006
Posts: 127
Hi Ranchers


I have a class that contains 2 synchronised methods.I have 2 threads .Can these 2 threads concurrently execute these 2 methods(ie. First thread on First synchronised method and Second thread on Second synchronised method at the same time)If not why??


"And the trouble is, if you don't risk anything, you risk even more." -- Erica Jong.
Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 18509
    
  40

Originally posted by sudhana madhu:
I have a class that contains 2 synchronised methods.I have 2 threads .Can these 2 threads concurrently execute these 2 methods(ie. First thread on First synchronised method and Second thread on Second synchronised method at the same time)If not why??


Whether two threads can run concurrently depends on the locks that they use to synchronize with. Static methods use the Class instance that represents the class. Non-static methods use the 'this' object.

So... If one method is static and the other non-static, they can run concurrently. Also, if neither method is static, and they call different instances, they can run concurrently.

If both methods are static, or if both methods are not static (with the same instance), then no, they can *not* run concurrently.

Henry


Books: Java Threads, 3rd Edition, Jini in a Nutshell, and Java Gems (contributor)
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Concurrent execution
 
Similar Threads
Thread
synchronization doubt
Monitor states and thread synchronization..
threads synchronized methods
Synchronised method