如何在不同的可运行线程Java中读取不同的文件

时间:2016-09-21 00:23:41

标签: java multithreading file runnable

我需要读取同时运行4个线程的文件夹中的所有.txt个文件。我能做什么,以便每个线程读取不同的文件。例如:线程1读取文件1,线程2读取文件2 ......依此类推,直到没有更多文件可读。

Thread h;


public Hilo(){
    h= new Thread(this,"Hilo 1");
    h.start();
}

public void run(){
    int contador;
    File folder = new File("C:/Users/Jose/Desktop/java");
    File[] listOfFiles = folder.listFiles();
    for (File file : listOfFiles) { 
        if (file.isFile()) {
            contador=0;
            int i=0;
            //System.out.println(file.getName());
            Scanner scan = null;
            try {
                scan = new Scanner (file);
            } catch (FileNotFoundException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
            while (scan.hasNext()){
                String linea=scan.next();
                String[] lineas = linea.split("(?=[,.])|\\s+");
                if (Palindrome.is(lineas[i])){
                    contador++;
                }
            }
            scan.close();

        }

    }
}

3 个答案:

答案 0 :(得分:0)

package z;

import java.io.File;
import java.io.FileNotFoundException;
import java.util.Scanner;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

public class Test {

    public static void main(String[] args) {

    ExecutorService exec = Executors.newFixedThreadPool(4);

    File folder = new File("C:/Users/Jose/Desktop/java");

    File[] files = folder.listFiles();

    for(File file : files){

        exec.execute(new Worker(file));
    }

    }
}

class Worker implements Runnable {

    private File file;

    public Worker(File file) {
    this.file = file;
    }

    @Override
    public void run() {
    int contador;
    if (file.isFile()) {
        contador = 0;
        int i = 0;
        Scanner scan = null;
        try {
        scan = new Scanner(file);
        } catch (FileNotFoundException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
        }
        while (scan.hasNext()) {
        String linea = scan.next();
        String[] lineas = linea.split("(?=[,.])|\\s+");
        if (Palindrome.is(lineas[i])) {
            contador++;
        }
        }
        scan.close();

    }

    }

}

答案 1 :(得分:0)

这是没有palindromo方法的代码,它不是必需的。

public class main {

    public static void main(String[] args) {
        // TODO Auto-generated method stub

        Hilo numero1 = new Hilo();
        Hilo numero2 = new Hilo();
        Hilo numero3 = new Hilo();
        Hilo numero4 = new Hilo();
    }
}
import java.io.File;
import java.io.FileNotFoundException;
import java.util.Scanner;

public class Hilo implements Runnable {

    Thread h;

    public Hilo() {
        h = new Thread(this, "Hilo 1");
        h.start();
    }

    public void run() {
        int c = 0;
        int contador;
        File folder = new File("C:/Users/Jose/Desktop/java");
        File[] listOfFiles = folder.listFiles();
        for (File file : listOfFiles) {
            if (file.isFile()) {
                contador = 0;
                int i = 0;
                //System.out.println(file.getName());
                Scanner scan = null;
                try {
                    scan = new Scanner(file);
                } catch (FileNotFoundException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
                while (scan.hasNext()) {
                    String linea = scan.next();
                    String[] lineas = linea.split("(?=[,.])|\\s+");
                    if (Palindromo.es(lineas[i])) {
                        contador++;
                    }
                    c++;
                }
                scan.close();
                System.out.println("La cantidad de Palindromos en el archivo " + file + " es de: " + contador);
            }

        }
    }
}

答案 2 :(得分:0)

试一试: 1.创建一个类ReadFiles:

public class ReadFiles {

    public interface IEvents {
        void onFile(File file);
    }
    int maxConcurrentThread = 5;
    ThreadPoolExecutor pool = null;

    volatile int countFiles = 0;
    String folder = "";
    IEvents event = null;

    public ReadFiles(String folder, int maxConcurrentThread) {
        this.folder = folder;
        this.maxConcurrentThread = maxConcurrentThread;
    }

    public void setEvent(IEvents event) {
        this.event = event;
    }

    public void read() {

        ThreadPoolExecutor pool = (ThreadPoolExecutor) Executors.newFixedThreadPool(this.maxConcurrentThread);

        File ffolder = new File(folder);
        File[] listOfFiles = ffolder.listFiles();
        for (final File file : listOfFiles) {
            if (file.isFile()) {
                pool.execute(new Runnable() {

                    @Override
                    public void run() {
                        countFiles++;
                        event.onFile(file);
                    }

                });
            }
        }

        pool.shutdown();
    }

}

使用方法:

int maxConcurrentThread = 5;
        ReadFiles read = new ReadFiles("C:/Users/Jose/Desktop/java", maxConcurrentThread);
        read.setEvent(new ReadFiles.IEvents() {

            @Override
            public void onFile(File file) {
                Scanner scan = null;
                try {
                    scan = new Scanner(file);
                } catch (FileNotFoundException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
                while (scan.hasNext()) {
                    String linea = scan.next();
                    String[] lineas = linea.split("(?=[,.])|\\s+");
                    if (Palindrome.is(lineas[i])) {
                        contador++;
                    }
                }
                scan.close();
            }
        });
        read.read();