wood burning stoves 2.0*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Object Locks Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Object Locks" Watch "Object Locks" New topic
Author

Object Locks

Ben Zaidi
Ranch Hand

Joined: Apr 08, 2007
Posts: 151
can one object have lock on two synchronized blocks at sametime.?


Ben,
--Lost in preparation of SCJP and SCWCD--
"Start writing a new chapter, for if you live by the book you'll never make history." (Ben Sobel)
Ankit Garg
Sheriff

Joined: Aug 03, 2008
Posts: 9313
    
  17

Only one thread can have a lock on an object at a time...


SCJP 6 | SCWCD 5 | Javaranch SCJP FAQ | SCWCD Links
Walsh graham
Greenhorn

Joined: Apr 10, 2009
Posts: 18
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

Joined: Aug 03, 2008
Posts: 9313
    
  17

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

Joined: Apr 08, 2007
Posts: 151
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

Joined: Aug 03, 2008
Posts: 9313
    
  17

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

Joined: Apr 08, 2007
Posts: 151
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

Joined: Apr 10, 2009
Posts: 18
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

Joined: Jul 15, 2009
Posts: 537

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.


[ SCJP 6.0 - 90% ] , JSP, Servlets and Learning EJB.
Try out the programs using a TextEditor. Textpad - Java 6 api
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: Object Locks