java线程的中断及控制

1、线程中断现象

Java的中断是一种协作机制。也就是说调用线程对象的interrupt方法并不一定就中断了正在运行的线程,它只是要求线程自己在合适的时机中断自己。

Thread类里的几个方法: 

public static boolean interrupted 测试当前线程是否已经中断。线程的中断状态 由该方法清除。换句话说,如果连续两次调用该方法,则第二次调用将返回 false(在第一次调用已清除了其中断状态之后,且第二次调用检验完中断状态前,当前线程再次中断的情况除外)。
public boolean isInterrupted() 测试线程是否已经中断。线程的中断状态 不受该方法的影响。
public void interrupt() 中断线程。

2、代码实现中断

package com.xxx.util;

/**
 * Created with IntelliJ IDEA.
 * Date: 15-3-26
 * Time: 上午9:50
 * To change this template use File | Settings | File Templates.
 */
public class ThreadInterrupt implements Runnable {

    private int num = 1;

    @Override
    public void run() {
        while (num<Integer.MAX_VALUE){
            if(isPrime(num)){
                System.out.printf("%d is prime\n",num);
            }
            num++;
        }
    }

    public boolean isPrime(long num){
        if(num<2){
            return false;
        }else{
            for(int i=2;i<num;i++){
                if(num%i==0){
                    return false;
                }
            }
        }
        return true;
    }

}
main类

package com.xxx.util;

/**
 * Created with IntelliJ IDEA.
 * Date: 15-3-26
 * Time: 上午10:20
 * To change this template use File | Settings | File Templates.
 */
public class ThreadInterruptMain {
    public static  void main(String[] args){
        try {
            ThreadInterrupt threadInterrupt = new ThreadInterrupt();
            Thread thread = new Thread(threadInterrupt);
            thread.start();
            thread.sleep(3000);
            thread.interrupt();
            if(thread.isInterrupted()){
                System.out.printf("Name:%s is interrupted\n",thread.getName());
                System.exit(0);
            }
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }
}
运行结果:


结论:

线程调用中断方法,中断状态变成true了,线程还是没有立刻终止,继续运行。每个线程都有一个boolean的中断状态(不一定就是对象的属性,事实上,该状态也确实不是Thread的字段),interrupt方法仅仅只是将该状态置为true。


3
、线程中断控制

package com.xxx.util;

import java.io.File;
import java.util.concurrent.TimeUnit;

/**
 * Created with IntelliJ IDEA.
 * Date: 15-3-26
 * Time: 下午1:52
 * To change this template use File | Settings | File Templates.
 */
public class ThreadInterruptControl extends Thread{

    private String filePath;
    private String fileName;

    public ThreadInterruptControl(String filePath,String fileName){
        this.filePath = filePath;
        this.fileName = fileName;
    }

    @Override
    public void run() {
        File file = new File(filePath);
        try{
            if(interrupted()) {
                throw new InterruptedException("正式处理前线程已经被请求中断");
            }
            if(file.isDirectory()){
                processDirectory(file);
            }
        }catch (InterruptedException e){
            System.out.printf("%s:The search has bean interrupted.exception:%s\n",Thread.currentThread().getName(),e.getMessage());
            //throw new RuntimeException(e);//throw异常则后面的语句不执行
        }
    }

    private void processDirectory(File file) throws InterruptedException{
        File files[] = file.listFiles();
        if(files!=null&&files.length!=0){
            for(int i=0;i<files.length;i++){
                File oneFile = files[i];
                if(oneFile.isDirectory()){
                    processDirectory(oneFile);
                }else{
                    processFile(oneFile);
                }
            }
        }
        if(isInterrupted()){
            throw new InterruptedException("directory searching  interrupt.");
        }
    }

    public void processFile(File file)throws InterruptedException{
        String oneFileName  = file.getName();
        if(oneFileName.equals(fileName)){
            System.out.printf("filePath:%s\n",file.getAbsolutePath());
            //TimeUnit.SECONDS.sleep(3);
        }
        if(isInterrupted()){
            throw new InterruptedException("file searching interrupt.");
        }
    }

}

main类

package com.xxx.util;

import java.util.concurrent.TimeUnit;

/**
 * Created with IntelliJ IDEA.
 * Date: 15-3-26
 * Time: 下午2:18
 * To change this template use File | Settings | File Templates.
 */
public class ThreadInterruptControlMain {

    public static void main(String[] args)throws InterruptedException{
        ThreadInterruptControl threadInterruptControl = new ThreadInterruptControl("F:\\VMware","vmware.log");
        Thread thread = new Thread(threadInterruptControl);
        thread.start();
        //TimeUnit.SECONDS.sleep(1);
        thread.interrupt();
        System.out.printf("%s 已调用线程中断方法。\n",thread.getName());
    }

}
运行结果:

结论:

只要可中断方法能检测到中断状态为true,就应该处理中断。那么自定义的可中断方法该如何处理中断呢?那就是在适合处理中断的地方检测线程中断状态并处理。 

检测中断有两种方法:

(1)Thread的静态方法interrupted,它将中断状态置为false,并将之前的状态返回;

(2)isInterrupted只是检测中断,并不改变中断状态。一般来说,处理过了中断请求,

应该将其状态置为false。但具体还要看实际情形。 

4、Java中断的本质 

协作式的中断,就是轮询某个表示中断的标记。JVM内部中断变量的主要优势,就是对于某些情况,提供了模拟自动“中断陷入”的机制。 在执行涉及线程调度的阻塞调用时(例如wait、sleep和join),如果发生中断,被阻塞线程会“尽可能快的”抛出InterruptedException。通过中断异常来处理中断处理工作。所谓“尽可能快”,我猜测JVM就是在线程调度调度的间隙检查中断变量,速度取决于JVM的实现和硬件的性能。 


参考:http://www.jb51.net/article/32359.htm


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值