aspose file tools*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Thread Ques 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 "Thread Ques" Watch "Thread Ques" New topic
Author

Thread Ques

Swati Gupta
Ranch Hand

Joined: May 28, 2002
Posts: 106
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

Joined: Jun 11, 2002
Posts: 25
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

Joined: Jun 13, 2002
Posts: 13
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


SJCP2
 
Consider Paul's rocket mass heater.
 
subject: Thread Ques