File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
Win a copy of Clojure in Action this week in the Clojure forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Class Lock in Threads

 
Shaila Vijaya Raja
Greenhorn
Posts: 23
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
janki tangeda
Ranch Hand
Posts: 54
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 242
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 813
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 ?
 
amit taneja
Ranch Hand
Posts: 813
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
ranchers any comments..pls
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic