Win a copy of Murach's Python Programming this week in the Jython/Python forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

Thread Ques  RSS feed

 
Swati Gupta
Ranch Hand
Posts: 106
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
From Jqplus

What will be the effect of interrupt() method here.
The answer says it will run forever.
I thought due to interrupt() the flag will set to true.
I am really doing very bad in thread ques in mock exams.
[ June 11, 2002: Message edited by: swati gupta ]
 
Abhijeet Gholkar
Greenhorn
Posts: 25
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
class A extends Thread
{
public void run()
{
System.out.println("Starting loop");
while(true)
{
if(isInterrupted())
{
System.out.println("Interrupting");
break;
}
}
}
}
public class TestClass
{
public static void main(String args[]) throws Exception
{
A a = new A();
a.start();
Thread.sleep(1000);
a.interrupt();
}
}
Try this code to putoff the thread
 
Bonnie Beach-Morin
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If your looking for a reason as to why it will run forever, it's because the interrupted() method doesn't actually do anything other than set a flag variable. The run method of class A never bothers to check the interrupted flag, so it doesn't know it should stop and runs forever.
Another solution would be to do the following
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!