• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Question On Object Locks.

 
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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?

 
Ranch Hand
Posts: 143
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Levon Escalona
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks for the reply Asha it really helped me out.
 
Ranch Hand
Posts: 83
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
reply
    Bookmark Topic Watch Topic
  • New Topic