在我的应用程序中,我正在执行一系列SQL查询,例如作业和执行的详细信息,这些查询都插入到表中。一旦插入任何记录,侦听器就应该选择它并开始监视执行状态并在同一个表中更新状态。
我的前端准备就绪,直到现在我能够执行作业(多个查询)并在表格中插入详细信息。现在我想捕获执行状态,并且我应该能够结束,暂停,重新开始执行。
当我经历多线程部分时,我遇到了ExecutorService。
我的问题是我应该使用ExecutorService吗?或者还有其他方式吗?
请分享任何教程链接。
答案 0 :(得分:1)
在您的情况下,我认为您可以使用ExecutorService并在Callable对象中创建作业,这些作业将帮助您以Future objects的形式获取正在运行的作业的状态。
对于教程我更喜欢this,它有关于并发包的基本信息。
答案 1 :(得分:0)
我使用ExecutorService来运行并行单元测试。并发现它非常成功。所以我建议你使用它。请参阅this
答案 2 :(得分:0)
也许这里我的问题的答案可以帮助捕捉任务的状态。
how-to-get-to-futuretask-execution-state
虽然Executors框架非常强大,几乎可以在任何情况下使用, 也许你应该看一下像Quartz 这样的并发库,如果使用Executors已经解决的话,你会遇到很多问题。
答案 3 :(得分:0)
是。您可以ExecuteService
,Callable
和Future
来实现这一目标。
示例代码:
import java.util.concurrent.*;
import java.util.*;
import java.util.Random;
public class CallableDemo{
public CallableDemo(){
System.out.println("creating service");
ExecutorService service = Executors.newFixedThreadPool(10);
System.out.println("Start");
List<MyCallable> futureList = new ArrayList<MyCallable>();
for ( int i=0; i<10; i++){
MyCallable myCallable = new MyCallable();
Future future = service.submit(myCallable);
try{
System.out.println("future.isDone = " + future.isDone());
/* You can use future.get() blocking call set time out*/
System.out.println("future: call ="+future.get());
}
catch (CancellationException ce) {
ce.printStackTrace();
} catch (ExecutionException ee) {
ee.printStackTrace();
} catch (InterruptedException ie) {
Thread.currentThread().interrupt(); // ignore/reset
}
}
service.shutdown();
}
public static void main(String args[]){
CallableDemo demo = new CallableDemo();
}
class MyCallable implements Callable<Long>{
Long result = 0L;
public MyCallable(){
}
public Long call(){
// Add your business logic
// set the result
return result;
}
}
}
主要提示:
InterruptedException
,请中断当前线程并继续。 shutdown()