aspose file tools*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Interrupt in Threads Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Interrupt in Threads" Watch "Interrupt in Threads" New topic
Author

Interrupt in Threads

Sireesha Mullapudi
Ranch Hand

Joined: Jun 26, 2006
Posts: 74
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

Joined: Aug 03, 2002
Posts: 7729
If you coded something then please present it here (formatted between tags). Thread.interrupt() will interrupt a sleeping thread if you use it properly.


Ask a Meaningful Question and HowToAskQuestionsOnJavaRanch
Getting someone to think and try something out is much more useful than just telling them the answer.
Sireesha Mullapudi
Ranch Hand

Joined: Jun 26, 2006
Posts: 74
Hi,


yes,You are Correct.I got it.

Thank You.
Sireesha Mullapudi
Ranch Hand

Joined: Jun 26, 2006
Posts: 74
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

Joined: Aug 03, 2002
Posts: 7729
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

Joined: Jun 26, 2006
Posts: 74
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

Joined: Apr 25, 2005
Posts: 809
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


Asking Smart Questions FAQ - How To Put Your Code In Code Tags
Sireesha Mullapudi
Ranch Hand

Joined: Jun 26, 2006
Posts: 74
Hi,

I got it.

Thanks
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Interrupt in Threads