Win a copy of Re-engineering Legacy Software this week in the Refactoring forum
or Docker in Action in the Agile forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Entangled in Threads

 
Umamaheswaran BS
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
In locking and synchronisation, We have the following text from K&B
:
"a thread can enter
a synchronized method, thus acquiring a lock, and then immediately invoke a
synchronized method on a different object, thus acquiring that lock as well...."
Now with a code please explain me clearly what is the object that is being talked about here...
 
Karol Kisielewski
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Umamaheswaran BS:
In locking and synchronisation, We have the following text from K&B
:
"a thread can enter
a synchronized method, thus acquiring a lock, and then immediately invoke a
synchronized method on a different object, thus acquiring that lock as well...."
Now with a code please explain me clearly what is the object that is being talked about here...


class A {
public synchronized void foo() {}
}

class B {
private A a = new A();

public synchronized void bar() {
a.foo();
}

public static void main(String[] args) {
B b = new B();
b.bar();
}
}
 
marc weber
Sheriff
Posts: 11343
Java Mac Safari
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
"Umamaheswaran BS,"

Welcome to JavaRanch!

Please revise your display name to meet the JavaRanch Naming Policy. To maintain the friendly atmosphere here at the ranch, we like folks to use real (or at least real-looking) names, with a first and a last name. Initials are okay for the first, but not the last.

You can edit your display name here.

Thank you for your prompt attention, and enjoy the ranch!

-Marc
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic