Win a copy of Clojure in Action this week in the Clojure forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Interrupt in Threads

 
Sireesha Mullapudi
Ranch Hand
Posts: 74
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

can you explain how to interrupt thread externally,so that i can see the effect of interrupt.I tried to interrupt a thread while it is sleeping.But i didn't get any change there.
Please Help me.

Thanks
 
Barry Gaunt
Ranch Hand
Posts: 7729
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If you coded something then please present it here (formatted between tags). Thread.interrupt() will interrupt a sleeping thread if you use it properly.
 
Sireesha Mullapudi
Ranch Hand
Posts: 74
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,


yes,You are Correct.I got it.

Thank You.
 
Sireesha Mullapudi
Ranch Hand
Posts: 74
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

If i test like as follows,it is giving flase for isInterrupted() condition in catch Block.why?

catch(InterruptedException e)
{
System.out.println("interrupted");
System.out.println(isInterrupted());
}
 
Barry Gaunt
Ranch Hand
Posts: 7729
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
isInterrupted() should return true (if you are running in the interrupted thread). Without seeing your code I cannot offer any more help.
 
Sireesha Mullapudi
Ranch Hand
Posts: 74
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

This is My code.Iam getting false for isInterrupted() METHOD in catch Block.
Please Help me.

public class ThreadOne extends Thread
{
public static void main(String a[])
{
ThreadOne t=new ThreadOne();
t.setName("interrupt thread");
t.start();
t.interrupt();

}
public void run()
{
try
{
for(int i=0;i<10;i++)
Thread.sleep(1000);
System.out.println(interrupted());
if(isInterrupted())
{
System.out.println("interrupt status"+isInterrupted());
System.out.println(" interrupted");
}
}
catch(InterruptedException e)
{
System.out.println("interrupted");

System.out.println(this.isInterrupted());
}

finally
{
System.out.println("got something");
}
}
}


Thanks.
 
Naseem Khan
Ranch Hand
Posts: 809
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
interrupt() method interrupts the thread.

If this thread is blocked in an invocation of the wait(), or sleep() or join(), then you will get InterrputedException and the interrputed status of the thread will then be false. So calling isInterrupted() method in catch will return you false.

Here I am modifying your code. Here isInterrupted() method will return true when thread is actually interrputed.





Naseem
 
Sireesha Mullapudi
Ranch Hand
Posts: 74
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

I got it.

Thanks
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic