File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Threads and Synchronization and the fly likes Killing a process in sleep Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


JavaRanch » Java Forums » Java » Threads and Synchronization
Bookmark "Killing a process in sleep" Watch "Killing a process in sleep" New topic
Author

Killing a process in sleep

Sonal Chawla
Greenhorn

Joined: Apr 02, 2006
Posts: 6
Hi,

There is Class A which executes its main and goes to sleep .

public static void main(String[] args) {
while (true) {
try {
Thread.sleep(1000);
}
catch (InterruptedException ex) {
ex.printStackTrace();
} }
}

Is there any way through which i can kill that instance of A.

Thanks
Sonal
Stan James
(instanceof Sidekick)
Ranch Hand

Joined: Jan 29, 2003
Posts: 8791
Another thread can interrupt() it. From the JavaDoc on interrupt ...

public void interrupt()
...
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.


A good question is never answered. It is not a bolt to be tightened into place but a seed to be planted and to bear more seed toward the hope of greening the landscape of the idea. John Ciardi
Sonal Chawla
Greenhorn

Joined: Apr 02, 2006
Posts: 6
Can I use interrupt in some other class to kill that instance which went into sleep?
Peter Chase
Ranch Hand

Joined: Oct 30, 2001
Posts: 1970
You don't interrupt classes, you interrupt threads. Any object can call interrupt() on any thread (I think), but whether that thread takes any notice depends on what it's doing. If it's in sleep(), it will stop sleeping and throw InterruptedException.


Betty Rubble? Well, I would go with Betty... but I'd be thinking of Wilma.
Sonal Chawla
Greenhorn

Joined: Apr 02, 2006
Posts: 6
I want in Class B to kill main thread of Class A which went into infinite sleep. I am new to the topic.
Peter Chase
Ranch Hand

Joined: Oct 30, 2001
Posts: 1970
Your questions aren't making any sense, I'm afraid. I think you'd better go read some introductory tutorials on Java threads. You'll find one or more on java.sun.com and probably loads by Googling.
Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 18876
    
  40

Originally posted by Sonal Chawla:
I want in Class B to kill main thread of Class A which went into infinite sleep. I am new to the topic.


Classes don't have have "threads". Classes have methods which are executed by threads. And in the cases where the method is not static, there may be more than on instance of that class, with that method, that is executed by threads.

This means, it is possible for a class, (and individual instances), to have no threads executing its methods, at a particular time. And it is also possible to have hundreds of threads executing its methods, as a particular time.

And if there is more than one thread executing the methods of a class, there is no concept of "main" thread. The nickname "main thread" is given to the thread that calls the main method of the class that starts the program.

Henry


Books: Java Threads, 3rd Edition, Jini in a Nutshell, and Java Gems (contributor)
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Killing a process in sleep