This week's book giveaways are in the Java EE and JavaScript forums.
We're giving away four copies each of The Java EE 7 Tutorial Volume 1 or Volume 2(winners choice) and jQuery UI in Action and have the authors on-line!
See this thread and this one for details.
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Question On Object Locks. Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of The Java EE 7 Tutorial Volume 1 or Volume 2 this week in the Java EE forum
or jQuery UI in Action in the JavaScript forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Question On Object Locks." Watch "Question On Object Locks." New topic
Author

Question On Object Locks.

Levon Escalona
Greenhorn

Joined: Apr 07, 2006
Posts: 3
Good Day Im currently reading the K&B book and I happen to encounter this program on Chapter 9 (Threads) Here is the code:



My Question is:



On this part you class ThreadA needs to acquire the lock on Object b before it is able to enter the code right? But upon entering block it then calls method wait() on object b. Why does it need to do this? also on the run method of ThreadB there is a synchronized block where you need the lock of the class. when you called b.wait() it releases the lock on the object and waits until it gets notified. but there is only one instance of b. who is the one calling the notify method?

Asha Pathik
Ranch Hand

Joined: Feb 08, 2006
Posts: 143
Hi,

class ThreadA {
public static void main(String [] args) {
ThreadB b = new ThreadB("Thread B");
b.start();

synchronized(b) {
try {
System.out.println("Current Thread: "+Thread.currentThread().getName());
System.out.println("Waiting for b to complete...");
b.wait();
} catch (InterruptedException e) {}
}
System.out.println("Total is: " + b.total);
}
}

class ThreadB extends Thread {
int total;

public ThreadB(String s)
{
super(s);
}

public void run()
{
System.out.println("Current Thread: "+Thread.currentThread().getName()+" Started");
synchronized(this)
{
System.out.println("Current Thread: "+Thread.currentThread().getName()+"Inside Synchronized Block in Run");
for(int i=0;i<100;i++)
{
total += i;
}
notify();
}
}
}

Actually there are two threads, one is the main thread and second one is the ThreadB instance. wait() method is invoked by the main thread and since when wait() method is invoked on an object, the thread executing the code gives up its locks and goes to waiting (from K&B page 724). In this case the "main thread". The notify method is invoked by the ThreadB instance which I think you are clear about.

Asha


SCJP 1.5
Levon Escalona
Greenhorn

Joined: Apr 07, 2006
Posts: 3
Thanks for the reply Asha it really helped me out.
Changchun Wang
Ranch Hand

Joined: Feb 15, 2006
Posts: 83
Asha Pathik is right
but now we don't know which thread will executed fristly,it up to jvm
because one thread only get locks on b and then can exeute while the other thread stay in waiting pool-

notice we dont know whih thread will get Lock frist
 
jQuery in Action, 2nd edition
 
subject: Question On Object Locks.