jQuery in Action, 2nd edition*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes keyword synchronized Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "keyword synchronized" Watch "keyword synchronized" New topic
Author

keyword synchronized

jeena jose
Ranch Hand

Joined: May 06, 2001
Posts: 69
Barry Boone in page 52 stating that
if a thread enters a synchronized instance method,no other thread can invoke any other synchronized instance method for that object.if a thread enters a synchronized static method,no other thread can invoke any other synchronized static method.

this means if a thread enters a synchronized instance method other tread can enter synchronize static methods of that object,
and also if a thread enters a synchronized static method,other thread can enter to any method other than static synchronized methods of that object.i feel it's wrong.am i right?
John Dale
Ranch Hand

Joined: Feb 22, 2001
Posts: 399
That's the way it works.
When you need more control than synchronized methods give you, see the synchronized statement.
jeena jose
Ranch Hand

Joined: May 06, 2001
Posts: 69
dear john,
as per my knowledge, if a thread enters a synchronized instance method,then no other thread can enter any other synchronized method including static synchronized methods of that object.(am i right?)but can access other methods(not synchronized)
and if a thread enters a static synchronized method,it get a lock on class object and no other thread can enter any methods on any object of that class.(am i right?)
i feel boone's statement wrong.
please help me!!i am weak in this topic.
jeena jose
William Brogden
Author and all-around good cowpoke
Rancher

Joined: Mar 22, 2000
Posts: 12791
    
    5
static synchronized methods are synchronized on the Class object - this is completely separate from any instances of the object. Having a lock on the Class object does not interfere with other Threads accessing instance methods of class instances and vice-versa.
So - your statement:
"and if a thread enters a static synchronized method,it get a lock on class object and no other thread can enter any methods on any object of that class."
is incorrect.
Bill

------------------
author of:
jeena jose
Ranch Hand

Joined: May 06, 2001
Posts: 69
brogden,
thanks for your reply.
i got an idea of synchronized keyword for class methods.
i am not getting any idea about this from JLS
is my first statement correct
"if a thread enters a synchronized instance method,then no other thread can enter any other synchronized method including static synchronized methods of that object.(am i right?)but can access other methods(not synchronized)"
jeena jose
William Brogden
Author and all-around good cowpoke
Rancher

Joined: Mar 22, 2000
Posts: 12791
    
    5
"if a thread enters a synchronized instance method,then no other thread can enter any other synchronized method including static synchronized methods of that object."
No you are wrong.
Like I said, the static methods are synchronized on a class object, - entirely separate from any instance object. Having a lock on an object affects only that object. Where did you get the idea that the above was correct??
jeena jose
Ranch Hand

Joined: May 06, 2001
Posts: 69
Brodgen,
thanks for reply.
i write what i understood.
if a thread enters a synchronized instance method no other thread can invoke any other synchronized instance method of that object,but can invoke other methods(even static synchronized or not with sychronized keyword).
if a thread enters a synchronized stratic method,no other thread can invoke any other synchronized stratic method for that class.but can invoke synchronized instance methods and other methods.
correct me if i am wrong.
i missed that static is on class not on object.
Maha Anna's this discussion only confused me.
Scenario - 4
-------------
- Assuming there is another static synchronized method introduced in the
above class like this
class MyClass {
synchronized void instSyncMethod1() {}
synchronized void instSyncMethod2() {}
void ordinaryMethod() {}
static synchronized void staticSyncMethod1() {}
}
- This staticSyncMethod1() obtains the lock associated with the Class object
for this Myclass as a whole. It is same as the foll
synchronized (Class.forName("MyClass")) {
}
- Assuming Thread1 calls this staticSyncMethod() on object1, then NO OTHER
THREAD can access ANY METHOD on ANY OTHER OBJECT including the object used
by Thread1, UNTIL this Thread1 releases this Class lock.
- This means NO OTHER Thread can call any of the methods in this object,
or any method on any other NEW objects of this SAME class.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: keyword synchronized