• 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

Threads notify() question: why synch with "this"?

 
Ranch Hand
Posts: 81
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,

Considering the following code:

I understand that T1, after starting T2, has to obtain T2's lock, so has to synch with T2, and then get on T2's waiting list by calling T2's wait() method.
But what confuses me is, why would the run() method in T2 have to synch with "this", which is like synching with itself, to call notify()? All I know is if I do not synch with "this", then I'll get a runtime exception (IllegalMonitorStateException).
So, please correct me if I'm wrong, to call notify(), a thread has to obtain it's own lock? If so, why?

Thanks,
-Vijay
 
author
Posts: 23951
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

I understand that T1, after starting T2, has to obtain T2's lock, so has to synch with T2, and then get on T2's waiting list by calling T2's wait() method.
But what confuses me is, why would the run() method in T2 have to synch with "this", which is like synching with itself, to call notify()? All I know is if I do not synch with "this", then I'll get a runtime exception (IllegalMonitorStateException).
So, please correct me if I'm wrong, to call notify(), a thread has to obtain it's own lock? If so, why?



There are two ways to answer this...

The simple answer is that it is a requirement of the API. You need to own the lock of the object that you are using to wait() or notify() with.


The really complex answer is because there is an inherent race condition related to wait() and notify(). You should not be using wait() and notify(), by itself, without some sort of flag. If you do, there will be cases where notifications will get lost.

Unfortunately, there is no way to protect the flag without integrating the freeing of the synchronization lock into the wait() method. This is why wait() mandates that the lock be owned. And for completion, since you are supposed to manage the lock anyway, notify() also mandates it.


This leads to another question. If I know the simple reason, and only grab the lock because the API mandates it, but don't know anything about the race condition, is it possible that I could get wait() and notify() to compile correctly, but the program won't be working?

Unfortunately, the answer is yes. You should spend some time with the Sun tutorial on threads, to fully understand wait() and notify().

Henry
 
Vijay Gade
Ranch Hand
Posts: 81
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thank you very much.

-Vijay
 
Ranch Hand
Posts: 67
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,
The reason for using a synchronized block with "this" argument is that the Thread t2 will be executed by an independent thread which is different from the thread being executed by Thread1. When the independent thread executes, a lock will be held over the current thread object by the synchronized block. this will prevent the synchorized block in thread1 from acquiring monitor of the Thread object. Let me know if this helps.

Regards
Abhijit
 
Vijay Gade
Ranch Hand
Posts: 81
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I'm not sure if I understood your explanation, and please forgive me for that.

I agree that Thread1 and T2 are independent threads. That was where Thread1 had to obtain a lock on T2 in order call the wait method on T2 (so that Thread1 can get into T2's waiting list).


The reason for using a synchronized block with "this" argument is that the Thread t2 will be executed by an independent thread which is different from the thread being executed by Thread1


Are you saying that the run() method of T2 is a different thread than that started in Thread1? Like for example, Thread1 creates a thread T2, schedules it (t2.start()), and then, when the run() method gets invoked, then a different thread TX will get executed?

Thanks,
-Vijay
 
Henry Wong
author
Posts: 23951
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

I'm not sure if I understood your explanation, and please forgive me for that.



I am not sure if I completely understood the explanation either...


Basically, the wait() and notify() mechanism uses objects to send and recieve notification -- meaning that if I call notify() on an object, then one of the threads that called wait(), on that object, is a candidate to recieve it.

In the example, the main thread (that started the t2 thread) is using the thread object that is used to represent the t2 thread, to wait on. In theory, it can use almost any object, as long as all threads have access to it, and they pick the same object.

Anyway, from the "point of view" of the t2 thread, the thread that is current running the run() method, this object happens to be the "this" object -- meaning the reason "this" is used, is because it happens to be the same object that the main thread used.

Henry
[ March 21, 2006: Message edited by: Henry Wong ]
 
Vijay Gade
Ranch Hand
Posts: 81
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Cool. This makes a lot more sense.

Thanks once again,
-Vijay
reply
    Bookmark Topic Watch Topic
  • New Topic