Help coderanch get a
new server
by contributing to the fundraiser
  • 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

notify()

 
Ranch Hand
Posts: 219
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi all....The program below compiles and runs but it hangs
How can i use the notify() to make the program end.
It will be great if some one helps me to solve this.


Thanks in advance.
 
Ranch Hand
Posts: 66
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
See solution . I have added certain things to make the code work.


The thread calls wait() and get into the waiting queue of the object f. but no one wakes it up. so we call notify() from main to wake this thread.

Output
before Wait
after start
after acquire lock
after Wait
after synchronized over

asakta@gmail.com
[ February 20, 2005: Message edited by: Aruna Agrawal ]
 
Ranch Hand
Posts: 35
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
How It can be checked that thread while in Thread.sleep(10000*1) has been interuppted and goes to catch block? Thanks in advance.
 
Ranch Hand
Posts: 298
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Veer,

When any other thread calls the method interrupt() the thread which is sleeping gets interrupted and then the catch block is called


The output of above code is "I was in sleep and now i am interrupted...."

Hope it makes u clear, for more info look at the api
 
meena latha
Ranch Hand
Posts: 219
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Animesh
In your program you when used t1.run(),it doesnt starts a new thread.
So when i replaced it with t1.start()the program hangs.

Kindly slove it..
 
Animesh Shrivastava
Ranch Hand
Posts: 298
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Ramya,
I am extremely sorry about the above program. Thats a totally wrong program. The correct one is here:

The explanation about the interrupt is explained as in API


If this thread is blocked in an invocation of the wait(), wait(long), or wait(long, int) methods of the Object class, or of the join(), join(long), join(long, int), sleep(long), or sleep(long, int), methods of this class, then its interrupt status will be cleared and it will receive an InterruptedException.





Accoridng to API, it says is that if the thread is in sleep state, and if interrupt is called on this thread, its interrupt status gets cleared and the thread is interruped
Hope now its clear
 
Every time you till, you lose 30% of your organic matter. But this tiny ad is durable:
We need your help - Coderanch server fundraiser
https://coderanch.com/t/782867/Coderanch-server-fundraiser
reply
    Bookmark Topic Watch Topic
  • New Topic