The moose likes Threads and Synchronization and the fly likes synchronizing a method Big Moose Saloon
  Search | Java FAQ | Recent Topics
Register / Login
JavaRanch » Java Forums » Java » Threads and Synchronization
Reply Bookmark "synchronizing a method" Watch "synchronizing a method" New topic
Author

synchronizing a method

Mathew Kuruvilla
Ranch Hand

Joined: Nov 27, 2001
Posts: 135
When we use
public class test {
synchronized aMethod1() {
}
aMethod2() {
}
}
for a method, we obtain a lock on the instance of the class where that code is executing. Am I right?
In this case, can we execute a call aMethod1 and aMethod2 concurrently and have them both execute the code concurently? Or would aMethod2 have to wait till that instance completes the method call to aMethod1?
Thanks.
Thomas Thomas
Greenhorn

Joined: Jun 17, 2003
Posts: 28
Hi
u r right the lock is obtained on instance of the class /Object..
u can execute a sync method & non sync method same time using multiple threads.
but if a class has two sync methods only one can be run at a time.
if u r using a complex method in which a small part is to be synchrinized .u can synchronized blocks
synchronized(this/object){
}
u can use blocks of code in method rather than sync the whole method
thomas
 
IntelliJ Java IDE
 
subject: synchronizing a method
 
Threads others viewed
When to use "thread-safe" classes?
hi a simple query
Super class as a reference type why?advantage?
local inner class acces .. help !
question about the class declaration.
IntelliJ Java IDE