java interrupt

See following tutorial from sun:

An interrupt is an indication to a thread that it should stop what it is doing and do something else. It's up to the programmer to decide exactly how a thread responds to an interrupt, but it is very common for the thread to terminate. This is the usage emphasized in this lesson.

So if a thread is interrupted, it maybe continue to run without stop. Whether it will stop is up to the programmer.

See the example below: ( I copied from http://hapinwater.iteye.com/blog/310558)

class ATask implements Runnable{   
  
    private double d = 0.0;   
       
    public void run() {   
        //死循环执行打印"I am running!" 和做消耗时间的浮点计算   
        while (true) {   
            System.out.println("I am running!");   
               
            for (int i = 0; i < 900000; i++) {   
                d =  d + (Math.PI + Math.E) / d;   
            }   
            //给线程调度器可以切换到其它进程的信号   
            Thread.yield();   
        }   
    }   
}   
  
public class InterruptTaskTest {   
       
    public static void main(String[] args) throws Exception{   
        //将任务交给一个线程执行   
        Thread t = new Thread(new ATask());   
        t.start();   
           
        //运行一断时间中断线程   
        Thread.sleep(100);   
        System.out.println("****************************");   
        System.out.println("Interrupted Thread!");   
        System.out.println("****************************");   
        t.interrupt();   
    }   
}   

 
The result shows:

......   
I am running!   
I am running!   
I am running!   
I am running!   
****************************   
Interrupted Thread!   
****************************   
I am running!   
I am running!   
I am running!   
I am running!   
I am running!   
....  

 

Check here for more information about interrupt in java:

http://hapinwater.iteye.com/blog/310558 

你可能感兴趣的:(java,thread,Blog,UP,sun)