簡體   English   中英

線程未同時運行以讀取文件

[英]Threads are not running simultaneously to read files

我想通過多線程讀取多個文件,我為相同的代碼編寫了代碼,但是我的線程正在一個接一個地執行,這非常耗時。 我希望它們同時運行。

請更正我在以下代碼中做錯的事情,其中​​通過實現可調用接口來執行此操作,因為我必須讀取文件並將其數據設置為Model對象的變量,然后再返回對象列表。

提前致謝。

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()) {

                        }

}

任務類別:

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
    }
  }
}

我看不到通過多線程讀取多個文件。 我看到ExecuterService調用了一個任務,它正在讀取所有文件。 多線程功能是通過將多個任務提交到ExecuterService來實現的,每個任務都被賦予一個文件進行處理(可以由構造函數執行)。

我認為這是您應該做的:

在內部for循環中,您構造一個在構造函數中指定給outputFileName的任務,並將其提交給執行程序,以返回Future實例。 提交所有任務后,您將擁有一個List<Future> ,您可以查詢該List<Future>何時完成任務並獲得結果。 該任務將調用readFiles() (讀取一個文件的方法的奇數名稱...)

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM