• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Doubt In Thread Topic

 
anil kumar
Ranch Hand
Posts: 447
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
what is the meaning of this statement?

A thread acquiring the lock of a class to execute a static synchronized method,has no bearing on any thread acquiring the lock on any object of the class to execute a synchronized instance method.

please tell me
 
Chandra Bhatt
Ranch Hand
Posts: 1710
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

A thread acquiring the lock of a class to execute a static synchronized method,has no bearing on any thread acquiring the lock on any object of the class to execute a synchronized instance method.


Hi Anil,

class DemoThread extends Thread{

public void run() {

}

public synchronized static void doStuff1() { //Lock of the class

}

public void doStuff2() {
synchronized(this) { //Lock of the instance of the class
....
}
}

public static void main(String... args) {

}

}

Inside the static method, there is no "this". So a thread acquiring lock on the static method has no bearing of the lock of the syncrhonized instance method. So while a thread executing a static synchronized method, it has no bearing on any thread acquiring the lock on any object of the class to execute a synchronized instance method.

Got it?

Thanks and Regards,
cmbhatt
[ March 31, 2007: Message edited by: Chandra Bhatt ]
 
anil kumar
Ranch Hand
Posts: 447
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
that means
if a thread got a lock on static Synchronized method.
other threads can execute the non static synchronized methods
is it right?

[ March 31, 2007: Message edited by: anil kumar ]
[ March 31, 2007: Message edited by: anil kumar ]
 
Chandra Bhatt
Ranch Hand
Posts: 1710
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,


Meanwhile try this code,



Do you come to any conclusion after executing this code?
"While a thread is executing static block of code, other threads still can access the synchronized instance methods, and why only synchronized all other methods too except static methods because there is one and only lock of the static method per class"


Thanks and Regards,
cmbhatt
 
Chandra Bhatt
Ranch Hand
Posts: 1710
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Anil,

After editing the post, you are correct. You corrected yourself.



Thanks and Regards,
cmbhatt
[ April 01, 2007: Message edited by: Chandra Bhatt ]
 
anil kumar
Ranch Hand
Posts: 447
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks
i got it.
 
Srinivasan thoyyeti
Ranch Hand
Posts: 557
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Anil,

You got it what Chandra said.

Interesting point.

1)// Class Lock is Unique. If one thread obtains it. No other thread in that JVM can obtain that lock until THE thread releases it.

synchronized(Sample.class){
}

2)//Object lock may not be unique. There is a possibility at runtime that SampleObjectRef point to some other Object.

synchronized(SampleObjectRef){
}

Concentrate on threads.
All the Best.
 
anil kumar
Ranch Hand
Posts: 447
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi

Srinivasan

yes i got it.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic