Win a copy of Clojure in Action this week in the Clojure forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Object Locks

 
Ben Zaidi
Ranch Hand
Posts: 151
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
can one object have lock on two synchronized blocks at sametime.?
 
Ankit Garg
Sheriff
Posts: 9497
22
Android Google Web Toolkit Hibernate IntelliJ IDE Java Spring
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Only one thread can have a lock on an object at a time...
 
Walsh graham
Greenhorn
Posts: 18
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
yes. Object A, B and C exist. A is the caller. B and C contain methods syncB() and syncC() which are both synchronized. This it is possible for A to call syncB() and syncC(), holding locks for B and C at the same time.

G
 
Ankit Garg
Sheriff
Posts: 9497
22
Android Google Web Toolkit Hibernate IntelliJ IDE Java Spring
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Walsh graham wrote:yes. Object A, B and C exist. A is the caller. B and C contain methods syncB() and syncC() which are both synchronized. This it is possible for A to call syncB() and syncC(), holding locks for B and C at the same time.


If it is to be viewed in that respect, then you can say that a Thread can hold lock to more than one objects. Objects don't hold lock to other objects...
 
Ben Zaidi
Ranch Hand
Posts: 151
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Actually, my question is like this. I am studying K & B, 9th Chapter about threads. Here is an example from K & B

1. class ThreadA {
2. public static void main(String [] args) {
3. ThreadB b = new ThreadB();
4. b.start();
5.
6. synchronized(b) {
7. try {
8. System.out.println("Waiting for b to complete...");
9. b.wait();
10. } catch (InterruptedException e) {}
11. System.out.println("Total is: " + b.total);
12. }
13. }
14. }
16. class ThreadB extends Thread {
17. int total;
18.
19. public void run() {
20. synchronized(this) {
21. for(int i=0;i<100;i++) {
22. total += i;
23. }
24. notify();
25. }
26. }
27. }

Now my confusion is this, first i am trying to track the flow of this example. What i can understand is.
There are two threads, One is thread main and other is ThreadB. When we start the program, two threads
will start the execution simultenously. When ThreadA reaches line 6, it gets the lock on Object B, which means,
that any other thread trying to acquire the lock on B will have to wait, until ThreadA releases the lock. In that case,
when threadA enters the synchronized block, and it calls b.wait(), then it releases the lock and flow goes to run()
method of ThreadB, which means that ThreadB will start executing and ThreadA will be waiting, till ThreadB doesn't
notify it back. It means that, a only one thread can have lock on an object at a time which is signified from this example.
Or if ThreadB first acquires the lock on object b, then in anycase, ThreadA won't be able to access the synchronized block at all?
Is my understanding correct?
 
Ankit Garg
Sheriff
Posts: 9497
22
Android Google Web Toolkit Hibernate IntelliJ IDE Java Spring
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
First of all Ben please Use Code Tags when you post a source code.

You've understood it correct. Although in this example, if threadB first occupies the lock on object b, then main will not be able enter its synchronized block till threadB releases the lock. After threadB releases the lock, main will enter the synchronized block and will start waiting and will never be notified by anyone so it will wait forever.

In any case you can observe this behavior using a program like this



When you run this code, you'll be able to see that main is not able to enter the synchronized block till the run method in ThreadB completes...
 
Ben Zaidi
Ranch Hand
Posts: 151
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Ankit. I understood the example. It was a great explanation.
In end, i am just too confused about threads. Should i study some other reference also.
I am not able to grasp many important concepts till now.

 
Walsh graham
Greenhorn
Posts: 18
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I would agree with the reply from Ankit Garg. "Java Threads" by Scott Oaks and Henry Wong is quite good for threads. I reccommend it.

Have a nice day
 
Nitish Bangera
Ranch Hand
Posts: 537
Eclipse IDE Java Python
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
K&B explains the concepts of concurrency very well. Well about synchronization, you can think of a scenario of a room with a computer. And there are some people who want to use the computer. So if the administrator comes, he enters the room and then lock the room from inside so that no one can enter. If some other normal person comes, he has to wait for the administrator to authorize him to use the computer. Also to use the computer he has to enter the room and then see that whether he is authorized or no. If he is not, he has to wait for the administrator to authorize him. Well this what i could break it down for cracking the concurrency questions the room and computer

Cheers.
 
Consider Paul's rocket mass heater.
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic