File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes notify() Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "notify()" Watch "notify()" New topic
Author

notify()

meena latha
Ranch Hand

Joined: Jan 24, 2005
Posts: 219
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.
Aruna Agrawal
Ranch Hand

Joined: Jan 27, 2005
Posts: 66
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 ]
Veer Batra
Ranch Hand

Joined: Mar 12, 2001
Posts: 35
How It can be checked that thread while in Thread.sleep(10000*1) has been interuppted and goes to catch block? Thanks in advance.
Animesh Shrivastava
Ranch Hand

Joined: Jul 19, 2004
Posts: 298
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

Joined: Jan 24, 2005
Posts: 219
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

Joined: Jul 19, 2004
Posts: 298
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
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: notify()
 
Similar Threads
Multithreading: Are two threads performing the same job?
notifyAll() not working
Need practical example of wait() and notify()
Important, about threads
Reg. Threads