[英]Threads are not running simultaneously to read files
I want to read multiple files through multi threading I wrote the code for the same but my threads are executing one by one which is very time consuming. 我想通过多线程读取多个文件,我为相同的代码编写了代码,但是我的线程正在一个接一个地执行,这非常耗时。 I wants them to run simultaneously.
我希望它们同时运行。
Please correct me what I am doing wrong in the below code where I am doing this by implementing the callable interface because I have to read the file and set its data into the variable of Model object and after that I am returning the list of objects. 请更正我在以下代码中做错的事情,其中通过实现可调用接口来执行此操作,因为我必须读取文件并将其数据设置为Model对象的变量,然后再返回对象列表。
Thanks In advance. 提前致谢。
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()) {
}
}
Class for task: 任务类别:
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
}
}
}
I do not see read multiple files through multi threading. 我看不到通过多线程读取多个文件。 I see one task invoked by the
ExecuterService
and it is reading all the files. 我看到
ExecuterService
调用了一个任务,它正在读取所有文件。 the multi threading feature is achieved by submitting multiple tasks to the ExecuterService
, each is given one file to process (can be by constructor). 多线程功能是通过将多个任务提交到
ExecuterService
来实现的,每个任务都被赋予一个文件进行处理(可以由构造函数执行)。
Here is what I think you should do: 我认为这是您应该做的:
inside the inner for loop, you construct a task that is given outputFileName
in constructor and submit it to the executor, getting back a Future
instance. 在内部for循环中,您构造一个在构造函数中指定给
outputFileName
的任务,并将其提交给执行程序,以返回Future
实例。 after all tasks were submitted, you will have a List<Future>
that you can query to see when they are done and get result. 提交所有任务后,您将拥有一个
List<Future>
,您可以查询该List<Future>
何时完成任务并获得结果。 the task will call readFiles()
(odd name for a method that reads one file...) 该任务将调用
readFiles()
(读取一个文件的方法的奇数名称...)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.