如何检查所有线程是否被中断

时间:2014-12-08 14:20:51

标签: java multithreading

我正在尝试使用多线程将所有文件从一个目录复制到另一个目录。我成功地这样做了。但我想跟踪所有线程是否被中断。

class testThread implements Runnable {
private Thread t;
private String threadName;
private File src;
private File dest;
boolean suspended = false;

testThread(String name,File source,File destination){
    threadName = name;
    src = source;
    dest = destination;
    System.out.println("Creating " +  threadName );
}

@Override
public void run() {
    System.out.println("Running " +  threadName );
    FileChannel inputChannel = null;
    FileChannel outputChannel = null;
    try{
        inputChannel = new FileInputStream(src).getChannel();
        outputChannel = new FileOutputStream(dest).getChannel();
        outputChannel.transferFrom(inputChannel,0,inputChannel.size());
        suspended = true;
        t.interrupt();
        System.out.println(threadName+"interrupted");
    } 
    catch (FileNotFoundException e) {
        e.printStackTrace();
    } 
    catch (IOException e) {
        e.printStackTrace();
    }
    finally{
        try {
            inputChannel.close();
            outputChannel.close();
        } 
        catch (IOException e) {
            e.printStackTrace();
        }
    }


}

public void start(){
    System.out.println("Starting " +  threadName );
    if(t==null){
         t = new Thread (this, threadName);
         t.start();
    }
}

public boolean isalive(){
    if(t.isInterrupted()){
        return false;
    }
    else{
        return true;
    }
}

public boolean isSuspended(){
    if(suspended==true){
        return true;
    }
    else{
        return false;
    }
}

}

public class Multithread2 {
   public static void main(String args[]) {

       File srcFolder = new File("C:\\Users\\xyz\\Desktop\\f1");
      // File destFolder = new File("C:\\Users\\xyz\\Desktop\\f2");

       File[] files = srcFolder.listFiles();
       System.out.println("files listed");

       testThread[] t=new testThread[files.length];

       boolean[] copy = new boolean[files.length];

       int i = 0;
       for(File f:files){

           String t1 = "t"+i;
           System.out.println(t1);
           File dest1 = new File("C:\\Users\\Desktop\\f2"+"\\"+f.getName());
           t[i] = new testThread(t1,(f.getAbsoluteFile()),new File("C:\\Users\\xyz\\Desktop\\f2"+"\\"+f.getName()));
           t[i].start();

           while(true){
               if((t[i].isalive())){
                   continue;
               }
               else{
                   break;
               }
           }
           i++;
       }


       if(t[i-1].isalive()==false){
           System.out.println("copying completed");
       }

   }  

在run方法结束时,我添加了t.interrupt()。所以我检查最后一个线程是否被中断。但我没有得到所需的输出。如果目录包含4个文件。然后控制台的内容就像这样

files listed
t0
Creating t0
Starting t0
Running t0
t0interrupted
t1
Creating t1
Starting t1
Running t1
t1interrupted
t2
Creating t2
Starting t2
Running t2
t2interrupted
t3
Creating t3
Starting t3
Running t3
copying completed
t3interrupted

所以在线程t3中断之前显示完成复制。

2 个答案:

答案 0 :(得分:1)

我建议使用java提供的ExecutorService,而不是为每个文件创建一个新线程。

这通常更干净,然后自己管理线程,并且可以让你等到所有任务(线程)都完成。

//dosent have to be files.length can be any value
ExecutorService pool = Executors.newFixedThreadPool(files.length);
//for each file
pool.execute(runnable);
//then wait until its all done
pool.awaitTermination(timeoutMillis, TimeUnit.MILLISECONDS);

答案 1 :(得分:0)

当它已经完成并且在出路时,没有任何意义可以让自己进行可运行的呼叫中断。使用isalive方法检查中断状态标志将没有用,因为一旦线程完成,标志就不会出现。

Thread类已经定义了一个isAlive方法,而是使用它。