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 ]
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
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
I agree. Here's the link: http://ej-technologies/jprofiler - if it wasn't for jprofiler, we would need to
run our stuff on 16 servers instead of 3.