• 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

Can interrupt() method is used to interrupt running thereads ...

 
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,
Can i use the interrupt() method to interrupt normal working threads or only for sleeping and waiting threads.
Please go through the below code,
----
public class D implements Runnable {
public void run(){
for (int i=0;i<100;i++){
System.out.println(i);
}
}
public static void main(String[] args) {
D d = new D();
Thread th = new Thread(d);
th.start();
th.interrupt(); // 1
}
}
---------
At // 1, i am interrupting the thread, 'th', but there is no effect.
Hi Friends,
Just now i found that one interesting thing. We all know the use of setPriority() method, which will be used to set the priority of the thread. You can pass the integer values or constants of the thread class(MIN_PRIORITY, MAX_PRIORITY). If you pass your own integer values it should be between 0 and 10, otherwise it will give IllegalArgumentException, even you can't pass 11.
That is interesting.
I hope many of you know this previously. Just i want to pass on others if some one not noted till now.( Bec till today i don't know)
Thanks in Advance,
 
Ranch Hand
Posts: 170
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I guess such errors are OS/JVM specific. Usually it is between 1 and 10. However these values may change based on the JVM implemementation for a specific OS.
Some time the MAX_PRIORITY might come down or might go up.
 
Ranch Hand
Posts: 3271
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Originally posted by Narasimha Rao B.:
Can i use the interrupt() method to interrupt normal working threads or only for sleeping and waiting threads.
...
At // 1, i am interrupting the thread, 'th', but there is no effect.

Yes, you can interrupt a thread that is not already blocked in some way. However, how it responds to that interruption is a little different.
If you look at the API Spec for API Spec for Thread, you'll find this under the description of the interrupt method:

If none of the previous conditions hold then this thread's interrupt status will be set.

That's saying that, if the thread is not blocking for some reason and received an interruption, it will set a status (basically sets a boolean variable to true). It's now up to the thread to check to see if it has been interrupted or not. I modified your test program a little to look like this:

I pulled this excerpt from the output:

As you can see, the main thread is sending an interruption to the spawned thread and the spawned thread can react to that interruption by utilizing the interrupted() method defined within Thread.
Note that you can also use isInterrupted() to determine if the thread has received an interrupt (as opposed to interrupted). However, interrupted() will reset the interrupted status to false while isInterrupted will leave it unchanged. You can look at the full descriptions of both within the API Spec.
I hope that helps,
Corey
 
Narasimha Rao B.
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 Corey,
Thanks for your reply.
Here is my understanding of the interrupt() method. (Please correct me, if i am wrong)
1. When you call interrupt() method on sleeping or waiting thread (sleeping or waiting)thread will throw the interrupted exception and will wakeup and will start working. And the boolean variable of the thread will be set to true.
2. When you call first time interrupt() method on normal working thread(say, going through big for loop), boolean variable(isInterrupted) will be set to true and will continue its usual execution. Note that, it will not stop execution because of calling the interrupt method. Any further calling interrupt method on the same thread will have not impact, because the boolean variable, is already true.
Is my understanding correct?
Please correct me, if i am wrong.

Thanks...
 
Corey McGlone
Ranch Hand
Posts: 3271
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I think you need to re-read the API Spec.


1. When you call interrupt() method on sleeping or waiting thread (sleeping or waiting)thread will throw the interrupted exception and will wakeup and will start working. And the boolean variable of the thread will be set to true.


If you interrupt a blocked thread, that thread will throw an InterruptedException so that the interruption can be handled. At that point, the interrupted status is not set. It is only set if the thread that is interrupted is not blocked.


2. When you call first time interrupt() method on normal working thread(say, going through big for loop), boolean variable(isInterrupted) will be set to true and will continue its usual execution. Note that, it will not stop execution because of calling the interrupt method. Any further calling interrupt method on the same thread will have not impact, because the boolean variable, is already true.


Be sure you don't confuse the interrupted status of a thread with the isInterrupted method.
If you invoke interrupt() on a thread that is not blocked, it's interrupted status will be set to true. It is then up to that thread to do something about that interruption - you can't force it to do something. Invoking interrupt on that thread will not have any further impact as the interrupt status is already set to true at this point. However, if that thread does handle the interruption by invoking its interrupted method (which resets the interrupted status to false), you can reset the status to true by again invoking interrupt on that thread.
I hope that helps,
Corey
 
Narasimha Rao B.
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 Corey,
Thanks a lot for such a nice reply. It really helps.

Thanks.
 
It runs on an internal combustion engine. This ad does not:
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