Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/371.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 线程没有同时运行以读取文件_Java_Multithreading_Parallel Processing - Fatal编程技术网

Java 线程没有同时运行以读取文件

Java 线程没有同时运行以读取文件,java,multithreading,parallel-processing,Java,Multithreading,Parallel Processing,我想通过多线程读取多个文件。我为同一个线程编写了代码,但我的线程正在逐个执行,这非常耗时。我希望它们同时运行 请纠正我在下面代码中的错误,我通过实现可调用接口来执行此操作,因为我必须读取文件并将其数据设置到模型对象的变量中,然后返回对象列表 提前谢谢 Class A{ ExecutorService executor = getExecuterService(); private ExecutorService getExecuterService() { int threa

我想通过多线程读取多个文件。我为同一个线程编写了代码,但我的线程正在逐个执行,这非常耗时。我希望它们同时运行

请纠正我在下面代码中的错误,我通过实现可调用接口来执行此操作,因为我必须读取文件并将其数据设置到模型对象的变量中,然后返回对象列表

提前谢谢

Class A{

ExecutorService executor = getExecuterService();

private ExecutorService getExecuterService() {
        int threadPoolSize = Runtime.getRuntime().availableProcessors() - 1;
        System.out.println("Number of COre" + threadPoolSize);
        return Executors.newFixedThreadPool(threadPoolSize);
    }

@SuppressWarnings({ "unused", "unchecked" })
                        FutureTask<List<DSection>> viewList = (FutureTask<List<DSection>>) executor
                        .submit(new MultiThreadedFileReadForDashboard(DashboardSectionList, sftpChannel,customQuery));

                        executor.shutdown();
                        while (!executor.isTerminated()) {

                        }

}
A类{
ExecutorService executor=getExecuterService();
私有ExecutorService getExecuterService(){
int threadPoolSize=Runtime.getRuntime().availableProcessors()-1;
System.out.println(“芯数”+螺纹池大小);
返回Executors.newFixedThreadPool(threadPoolSize);
}
@SuppressWarnings({“未使用”、“未选中”})
FutureTask视图列表=(FutureTask)执行器
.submit(新的多线程文件ReadForDashboard(仪表板分区列表、sftpChannel、customQuery));
executor.shutdown();
而(!executor.isTerminated()){
}
}
任务类:

public class MultiThreadedFileReadForDashboard implements Callable {

public MultiThreadedFileReadForDashboard(List<DSection> dashboardSectionList, ChannelSftp sftpChannel,
            CustomQueryImpl customQuery) {

        this.dashboardSectionList = dashboardSectionList;
        this.sftpChannel = sftpChannel;
        this.customQuery = customQuery;
    }

    public List<DSection> call() throws Exception {

        for (int i = 0; i < dashboardSectionList.size(); ++i) { 

            DSection DSection = dashboardSectionList.get(i);
            List<LView> linkedViewList = new ArrayList<LView>(DSection.getLinkedViewList());

            LView lView;

            for (int j = 0; j < linkedViewList.size(); ++j) {
                lView = linkedViewList.get(j);
                int UserQueryId = Integer.parseInt(lView.getUserQueryId());

                outputFileName = customQuery.fetchTableInfo(UserQueryId);


                if ((outputFileName != null) && (!outputFileName.equalsIgnoreCase(""))) {

                    String data = readFiles(outputFileName);
                    lView.setData(data);


                } else {
                    lView.setData("No File is present");
                }

            }
            if (size == dashboardSectionList.size()) {
                break;
            }
        }

        return dSectionList;
    }

    private String readFiles(String outputFileName) {
        String response = null;
        try {

            InputStream in = sftpChannel.get(outputFileName);
            BufferedReader br = new BufferedReader(new InputStreamReader(in, "UTF-8"));
            StringBuilder inputData = new StringBuilder("");
            String line = null;
                while ((line = br.readLine()) != null) {
                inputData.append(line).append("\n");
            }

            JSONArray array = null;

            if (outputFileName.toLowerCase().contains("csv")) {
                array = CDL.toJSONArray(inputData.toString());
            } else {    
            }
            response = array.toString();
        } catch (Exception e) {
            e.printStackTrace();
        }
        return response;
        // TODO Auto-generated method stub
    }
  }
}
公共类多线程FileReadForDashboard实现可调用{
公用多线程文件ReadForDashboard(列表仪表板部分列表,通道SFTP sftpChannel,
CustomQueryImpl customQuery){
this.dashboardSectionList=仪表板SectionList;
this.sftpChannel=sftpChannel;
this.customQuery=customQuery;
}
公共列表调用()引发异常{
对于(int i=0;i
我看不到通过多线程读取多个文件。我看到一个由ExecuterService调用的任务,它正在读取所有文件。多线程功能是通过向
ExecuterService
提交多个任务来实现的,每个任务都有一个要处理的文件(可以由构造函数处理)

以下是我认为你应该做的:


在内部for循环中,您构造了一个任务,该任务在构造函数中被赋予
outputFileName
,并将其提交给执行器,从而返回一个
Future
实例。提交所有任务后,您将有一个
列表
,您可以查询该列表以查看任务何时完成并获得结果。该任务将调用
readFiles()
(读取一个文件的方法的奇数名称…

不是答案,但您可能应该将您的可调用项转换为可调用项您只提交一个读取for循环中所有文件的任务。要实现并行处理,您需要提交多个任务…您是否从单个磁盘驱动器读取?如果是这样,如果磁盘是物理磁盘,则并行读取可能比顺序读取文件慢。文件系统试图存储文件而不将其碎片化,因此可以读取文件而不必到处寻找磁头。而磁头搜索确实可以降低IO速率。如果并行读取多个文件,则会强制磁头在整个磁盘表面进行查找。消费级SATA驱动器的速度可以达到每秒40或50次这样的寻道操作,甚至更低。