【问题标题】:How to run threads at the same time and access same resource in java如何在java中同时运行线程并访问相同的资源
【发布时间】:2018-12-07 12:22:36
【问题描述】:

下面我写了一个 runnable 来运行一个目录并解析其中的 csv 文件。当前代码运行,以便 T1 和 T2 读取目录中的所有文件。如何使 T1 和 T2 同时运行,但不能访问相同的文件进行解析..

public class ThreadDemo{
    public static void main(String[] args){
        ExecutorService executorService = Executors.newFixedThreadPool(10);
        executorService.execute(new Task());//T1
        executorService.execute(new Task());//T2
        executorService.shutdown();
    }
}

class Task implements Runnable {
    @Override
    public void run() {
        // TODO Auto-generated method stub
        try {
            String line = "";
            String dirName = "/dir/location";
            File[] fileList = new File(dirName).listFiles();
            BufferedReader br = null;
            for (File file :  fileList){
                br = new BufferedReader(new FileReader(file.getName()));
                while ((line = br.readLine()) != null){
                    //parse few vaule
                }
                System.out.println("The file name is this :::: "+file.getName());
            }
        }
        catch (Exception e)
        {
            e.printStackTrace();
        }
    }
}

【问题讨论】:

    标签: java multithreading file filereader


    【解决方案1】:

    我首先将Files 的数组拆分为 2 个子数组,并为每个任务分配一个子数组。像这样:

    public class ThreadDemo {
        public static void main(String[] args) {
            String dirName = "/dir/location";
            File[] fileList = new File(dirName).listFiles();
            int mid = fileList.length / 2;
            File[] fileListOne = Arrays.copyOfRange(fileList, 0, mid);
            File[] fileListTwo = Arrays.copyOfRange(fileList, mid, fileList.length);
            ExecutorService executorService = Executors.newFixedThreadPool(10);
            executorService.execute(new Task(fileListOne));//T1
            executorService.execute(new Task(fileListTwo));//T2
            executorService.shutdown();
        }
    }
    
    class Task implements Runnable {
        private final File[] fileList;
    
        public Task(File[] fileList) {
            this.fileList = fileList;
        }
    
        @Override
        public void run() {
            try {
                String line = "";
                for (File file : fileList) {
                    try (BufferedReader br = new BufferedReader(new FileReader(file.getName()))) {
                        while ((line = br.readLine()) != null) {
                            //parse few vaule
                        }
                        System.out.println("The file name is this :::: " + file.getName());
                    }
                }
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }
    

    【讨论】:

    • 可能是最简单的一个。如果线程 1 有更大的文件,负载将不会被平均分配。另一种方法是跟踪哪个线程正在处理哪个文件并跳过该文件。
    • 我同意。如果我们有更多线程(比如 4-5 个),我可能会有一个队列,线程将从该队列中获取下一个要处理的 File
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-04-23
    • 1970-01-01
    • 2014-01-18
    • 2012-10-11
    • 2015-07-06
    相关资源
    最近更新 更多