Win a copy of Programmer's Guide to Java SE 8 Oracle Certified Associate (OCA) this week in the OCAJP forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Killing a process in sleep

 
Sonal Chawla
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 8791
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Sonal Chawla
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Can I use interrupt in some other class to kill that instance which went into sleep?
 
Peter Chase
Ranch Hand
Posts: 1970
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Sonal Chawla
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 1970
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Marshal
Pie
Posts: 21362
84
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic