• 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
  • Tim Cooke
  • Liutauras Vilda
  • Jeanne Boyarsky
  • paul wheaton
Sheriffs:
  • Ron McLeod
  • Devaka Cooray
  • Henry Wong
Saloon Keepers:
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
  • Tim Moores
  • Mikalai Zaikin
Bartenders:
  • Frits Walraven

Q on threads: notify and notifyAll

 
blacksmith
Posts: 979
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Dear all,
With threads the method notifyAll() wakes up all threads
waiting for a particular objects lock. All the threads
will then compete for the lock. With notify() an arbitrary
thread is woken up.
Isn't the end result the same: one thread will have the lock.
So why two methods, isn't one of the two methods
enough?
Can anyone explain this?
Thanks,
Gian Franco Casula
 
Ranch Hand
Posts: 522
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I posted this question way back ago, I think it'll answer your doubt.
Check it out here
 
Gian Franco
blacksmith
Posts: 979
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks Vicken, that's exactly what I
was looking for. It's a very useful
discussion.
Greetings,
Gian Franco
 
Ranch Hand
Posts: 205
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,
When i was going through this topic, as Vicken referred i went through the topic, "notify() Vs notifyAll()" topic posted in Aug 2003, there was lot of interesting topics i found. In that Praveen Posted the below code.
-------------
1class TestThread extends Thread
2{
3public void startMe()
4{
5synchronized(this)
6{
7notify();
8System.out.println("Notified...");
9try
10{
11System.out.println("waiting in startME");
12wait();
13System.out.println("woke from wait in startME");
14
15}
16catch(InterruptedException e){}
17}
18}
19
20public void run()
21{
22try
23{
24synchronized(this)
25{
26System.out.println("waiting in run");
27wait();
28System.out.println("woke up from wait in run");
29}
30}
31catch(InterruptedException e){}
32}
33public static void main(String[] args)throws Exception
34{
35TestThread t1 = new TestThread();
36t1.start();
37Thread.sleep(1000);
38t1.startMe();
39}
40}
----------------
Please clarify my below doubt on above programme.
There was a wait() method called on line 12. After that statement, no where in the programme, notify() or notifyAll() method is invoked, then how that thread is waking from wait state.
Thanks in Advance,
Narasimha.
 
Screaming fools! It's nothing more than a tiny ad:
Gift giving made easy with the permaculture playing cards
https://coderanch.com/t/777758/Gift-giving-easy-permaculture-playing
reply
    Bookmark Topic Watch Topic
  • New Topic