ThreadpoolExecutor 和主线程并行执行
ThreadpoolExecutor and main thread executing in parallel
线程池执行器正在与主线程并行执行。主线程不会等到执行程序关闭。
public static void main(String[] args) {
Date jobStartTime = null;
LOGGER.info("MainApp::Job started");
try {
MainApp obj = new MainApp();
// Getting the job Id of the job
String jobName=args[0]; //batch name
String fileName=args[1]; //sqoop file
LOGGER.info("MainApp::jobName: "+jobName+" fileName "+fileName);
currentJobID = obj.getMaxJobId(jobName);
LOGGER.info("MainApp:Job Id is" + currentJobID);
// Getting the start time of the job
jobStartTime = commonDB.getTime();
LOGGER.info("MainApp:Job Start time is" + jobStartTime);
JobDetails job=new JobDetails(currentJobID,jobName,fileName);
// Reading and parsing the sqoop file and executing the sqoop commands
CommandGenerator exec=new CommandGenerator();
List<TableInfo> commandList = exec.parseAndExec(job);
ThreadPoolExecutor tp = (ThreadPoolExecutor) Executors.newFixedThreadPool(10);
for (final TableInfo table : commandList) {
ParallelExecutor pe = new ParallelExecutor(table);
tp.execute(pe);
}
tp.shutdown();
while(!tp.isShutdown()){
}
job=new JobDetails(currentJobID,jobName,fileName,jobStartTime);
//put everything in one method
StatusAndMailUtils status=new StatusAndMailUtils();
status.onJobCompletion(job);
} catch (Exception e) {
// TODO Auto-generated catch block
LOGGER.info("MainApp::Exception");
e.printStackTrace();
}
}
我已经使用 while 循环让主线程在执行程序线程进行时保持等待。但是,它没有帮助。请让我知道如何让主线程等待。
while(!tp.isShutdown()){
}
当然不是等待。这就是创建线程池的全部思路,因此您的主线程可以在线程池执行其他任务的同时执行其他任务。
您可以使用 awaitTermination(long timeout, TimeUnit unit) 方法让您的主线程在线程池完成其任务时暂停。
调用shutdown()
后,可以使用awaitTermination(long timeout, TimeUnit unit)
阻塞调用线程,直到所有任务执行完毕。
作为超时,如果您想等待任务完成所需的时间,您可以使用一个过大的值,但是如果任务永远不会结束,它可能会使您的线程永远等待,所以最好是设置一个合理的超时时间,以便在异常过长的情况下执行一些任务。
例如:
tp.shutdown();
tp.awaitTermination(Long.MAX_VALUE, TimeUnit.DAYS);
您也可以提交这些Runnable,等待它们完成。也可以在抛出异常之前指定超时以等待线程执行。
List<Future<ParallelExecutor>> tasks = new ArrayList<>();
ExecutorService tp = Executors.newFixedThreadPool(10);
for (final TableInfo table : commandList) {
ParallelExecutor pe = new ParallelExecutor(table);
tasks.add(tp.submit(pe));
}
for (Future<ParallelExecutor > p : tasks) {
p.get(); // with timeout p.get(10, TimeUnit.SECONDS);
}
tp.shutdown();
线程池执行器正在与主线程并行执行。主线程不会等到执行程序关闭。
public static void main(String[] args) {
Date jobStartTime = null;
LOGGER.info("MainApp::Job started");
try {
MainApp obj = new MainApp();
// Getting the job Id of the job
String jobName=args[0]; //batch name
String fileName=args[1]; //sqoop file
LOGGER.info("MainApp::jobName: "+jobName+" fileName "+fileName);
currentJobID = obj.getMaxJobId(jobName);
LOGGER.info("MainApp:Job Id is" + currentJobID);
// Getting the start time of the job
jobStartTime = commonDB.getTime();
LOGGER.info("MainApp:Job Start time is" + jobStartTime);
JobDetails job=new JobDetails(currentJobID,jobName,fileName);
// Reading and parsing the sqoop file and executing the sqoop commands
CommandGenerator exec=new CommandGenerator();
List<TableInfo> commandList = exec.parseAndExec(job);
ThreadPoolExecutor tp = (ThreadPoolExecutor) Executors.newFixedThreadPool(10);
for (final TableInfo table : commandList) {
ParallelExecutor pe = new ParallelExecutor(table);
tp.execute(pe);
}
tp.shutdown();
while(!tp.isShutdown()){
}
job=new JobDetails(currentJobID,jobName,fileName,jobStartTime);
//put everything in one method
StatusAndMailUtils status=new StatusAndMailUtils();
status.onJobCompletion(job);
} catch (Exception e) {
// TODO Auto-generated catch block
LOGGER.info("MainApp::Exception");
e.printStackTrace();
}
}
我已经使用 while 循环让主线程在执行程序线程进行时保持等待。但是,它没有帮助。请让我知道如何让主线程等待。
while(!tp.isShutdown()){
}
当然不是等待。这就是创建线程池的全部思路,因此您的主线程可以在线程池执行其他任务的同时执行其他任务。
您可以使用 awaitTermination(long timeout, TimeUnit unit) 方法让您的主线程在线程池完成其任务时暂停。
调用shutdown()
后,可以使用awaitTermination(long timeout, TimeUnit unit)
阻塞调用线程,直到所有任务执行完毕。
作为超时,如果您想等待任务完成所需的时间,您可以使用一个过大的值,但是如果任务永远不会结束,它可能会使您的线程永远等待,所以最好是设置一个合理的超时时间,以便在异常过长的情况下执行一些任务。
例如:
tp.shutdown();
tp.awaitTermination(Long.MAX_VALUE, TimeUnit.DAYS);
您也可以提交这些Runnable,等待它们完成。也可以在抛出异常之前指定超时以等待线程执行。
List<Future<ParallelExecutor>> tasks = new ArrayList<>();
ExecutorService tp = Executors.newFixedThreadPool(10);
for (final TableInfo table : commandList) {
ParallelExecutor pe = new ParallelExecutor(table);
tasks.add(tp.submit(pe));
}
for (Future<ParallelExecutor > p : tasks) {
p.get(); // with timeout p.get(10, TimeUnit.SECONDS);
}
tp.shutdown();