*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Class Lock in Threads Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Class Lock in Threads" Watch "Class Lock in Threads" New topic
Author

Class Lock in Threads

Shaila Vijaya Raja
Greenhorn

Joined: Apr 09, 2005
Posts: 23
hi,

we know that every object has a lock.

Like wise every class have a lock...? or not...?

If not, how the class is said to be locked when we access a static

synchronized method using an object for that class.

*Also if there is a class lock, then can i access a non synchronized method

using another object?
* Whether a class is said to be locked what happened to that corresponding

object, it also acquires the lock or not...?

please share your valuable ideas

shailavijayaraja


With Regards,<br />Shaila Vijaya Raja<br /> <br />SCJP1.4
janki tangeda
Ranch Hand

Joined: Jun 07, 2005
Posts: 54
hi,

Like wise every class have a lock...? or not...?

yes classes can also be locked.

If not, how the class is said to be locked when we access a static

synchronized method using an object for that class.

*Also if there is a class lock, then can i access a non synchronized method

using another object?

yes
* Whether a class is said to be locked what happened to that corresponding

object, it also acquires the lock or not...?

If the corresponding object is executing the static synchronized method of class then it means that it has acquired the class lock.If the object is executing the synchronized instance method then it implies that it has acquired the object lock.Aclass lock and an object lock are independent of each other.
Santhosh Kumar
Ranch Hand

Joined: Nov 07, 2000
Posts: 242
we know that every object has a lock. Like wise every class have a lock...? or not...?

umm... good question. Yes, every object has got a lock. Like wise, every class?? No... class definition does not have a lock on its own but the object which represent that class definition has got a lock.

There is class java.lang.Class. An instance of this class is created for each loaded class into the JVM. The instance of Class.java represent the definition of a class file.

So when you say that, you are taking lock on Class, its actually you are taking lock on this Class object which represent your own class. As there will be only one instance of Class.java for every class loaded into JVM, by taking lock on this object, you are ensure that only ONE thread will ever execute.
amit taneja
Ranch Hand

Joined: Mar 14, 2003
Posts: 810
in this eg.

class A extends Threads
{
public syncronized method1{ }
public syncronized mehtod2{ }
public void run(){
//do some stuff
}
public static void main(String args[])
{
A a1=new A();
A a2=new A();
}}

in the above code a1 and a2 are two objects ? isn't it. or two threads..

why we can't call them two objects... and if they are threads then only one can access method1 and method2 at one time ?

as they are syncronize...but we should say that class has one lock
which the object(thread in this case ?) take and no other syncronized method can be executed till lock of class relized...

i m confused w.r.t wording.... as we must say that class has one object which is aquired by thread or objects ??
what should we say in above case regarding class lock or object lock
are a1 adn a2 are different objects or they are threads ?


Thanks and Regards, Amit Taneja
amit taneja
Ranch Hand

Joined: Mar 14, 2003
Posts: 810
ranchers any comments..pls
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Class Lock in Threads
 
Similar Threads
Difference between class level lock and object lock
Threads Basics
Help: Basics of Thread
Can someone check my lock/unlock methods, please?
synchronized keyword use