Win a copy of Svelte and Sapper in Action this week in the JavaScript forum!
  • 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 all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Ron McLeod
  • Paul Clapham
  • Bear Bibeault
  • Junilu Lacar
Sheriffs:
  • Jeanne Boyarsky
  • Tim Cooke
  • Henry Wong
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • salvin francis
  • Frits Walraven
Bartenders:
  • Scott Selikoff
  • Piet Souris
  • Carey Brown

JQ+ Test10 1003499030687 - interrupt() method

 
Ranch Hand
Posts: 56
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
ha all ,
JQ+ 1003499030687 Test10:
=============================================
class A extends Thread
{
public void run(){
System.out.println("Starting loop");
while(true){};
}
}
public class TestClass
{
public static void main(String args[]) throws Exception
{
A a = new A();
a.start();
Thread.sleep(1000);
a.interrupt(); //!!!
}
}
==============================================
Given ans from JQ+ : it will run & never end.
==============================================
My question :
how does interupt() method works ?
& why it's not breaking this thread inside of
"while(true)" loop ?
Any help would be great
 
Ranch Hand
Posts: 48
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Try this code out, it might explain alot

class A extends Thread{
boolean t;
public void run(){
System.out.println("Starting loop");
while(!t){
System.out.println ("loop");
try{Thread.sleep(1000);}catch(InterruptedException ie){
t=true;
}
};
}
}
class TestClass
{
public static void main(String args[])
{
A a = new A();
a.start();
a.interrupt();
}
}

if you delete and undelete a.interrupt();
it should help you understand
 
Ivan Ivanoff
Ranch Hand
Posts: 56
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
THX Brent ! - got your point
while(!isInterrupted()){}
-works fine too .
THX!!!
 
Greenhorn
Posts: 18
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
When the interrupt() method is called on a Thread it enters the ready state. When it resumes running it will throw an InterruptedException. Thus in Brent's example the exception is caught and t is set to true, therefore ending the run method.
 
Ranch Hand
Posts: 165
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
So the point is: interrupt() works when you have a sleep() (or other methods that throw interrupted exception) method call in the "run" method. IF you do not have such method calls then the thread is simply put in a ready state provided there is no SecurityException.
Is that correct?
Thanks,
Brian
 
brent spearios
Ranch Hand
Posts: 48
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Tim is correct in what he says but if there is no method that throws an interrupted exception then interruptedexception will never be thrown, plus the thread is never put into a ready state as it is already there
 
    Bookmark Topic Watch Topic
  • New Topic