If in my Java class theere are 2 methods a() and b() and a() is synchronised when a thread is active on method a(). can any other thread access method b() which is not synchronised. So if 1 method is synchronised, then only that method is synchronised or the whole object Regards, aakash
Hi Aakash When you synchronize a method the lock is obtained for that object and when an object is locked all the synchronized methods are locked. That is when the lock is obtained on the class containing the a() and b() methods then only a() method would be locked and b() method or for that matter all unsynchronized methods would be available to others.
1)How can we lock the whole object to be accessed 2) And when we define the method aa synchronised where in memory it is stored. is it the same way static variabbles and methods are done as how if i synchronise one method it will lock the method of the object only for that instance or all the instance using this method will be locked Regards, aakash
1) Make all the methods synchronized, or at least all the public methods. 2) Where in memory is the mutex stored? Well, probably, generally, with the data for the object, on the heap; but this isn't specified by the Java Language Specification nor the VM specification. 3) Static synchronized methods use the mutex in the java.lang.Class object that represents the class; static synchronized methods thus are synchronized for all instances (or none, depending on how you look at it!)
1) Can the whole class be synchronized by implementing the SingleThreadModel interface 2) If i synchronized a method will that reside in Global memory as static methods reside. 3) What care we should take while declaring a method as synchronized if the other method in the class is not synchronized as the synchronized method may change some value which is not reflected in the other method which is not synchronized Regards, aakash
Can the whole class be synchronized by implementing the SingleThreadModel interface
The SingleThreadModel comes into picture when you work with servlets. Servlets are multi threaded by default i.e. several threads can invoke the doGet/doPost methods. In order to prevent muliple threads to access a single servlet instance you make your servlet SingleThreadModel i/f, which is an empty (marker) interface. You can make each method syncronized so that only one thread executes at a time. Make those methods synchronized so that your data is not corrupted. I hope this answers your last question.
1) Implementing SingleThreadModel is a signal to a Servlet container telling it how it is allowed to invoke a Servlet; it has no meaning to the JVM itself, and does nothing for any other classes. 2) I really don't know what you mean when you keep talking about "global memory" and where different things go. The Java memory model doesn't include any such concept, and it says nothing about where methods of any kind are stored. Why do you care? 3) If you make one method synchonized, but another not, and both methods access the same data, then what you have, generally, is a programmer error. Not sure why you're asking about "What care we should take" when you seem to already know how to use synchonization to protect data in a multithreaded application. What care you should take is to make both the methods synchronized if there are potential problems due to unsynchronized access!