Reputation: 27
i am trying to copy all the files from one directory to another using multithreading. I'm successfully doing so. But i want to track that whether all the threads are interrupted or not.
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");
}
}
At the end of run method, i am adding t.interrupt(). So i m checking for the last thread is interrupted or not. But i'm not getting the desired output.If the directory contains 4 files. Then the content of console is like this
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
so the copying completed is shown before the thread t3 is interrupted.
Upvotes: 0
Views: 59
Reputation: 383
Instead of creating a new thread for each file i would suggest using the ExecutorService provided by java.
This is usually cleaner then having to manage threads yourself and can give you the ability to wait until all the tasks(threads) have completed.
//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);
Upvotes: 2