我使用缓存线程池ExecutorService来运行一些异步后台任务.我已经提供了我的ThreadFactory,它将线程分发给ExecutorService(只要它需要它们).我对缓存线程池的理解是,在线程空闲60秒后,它由ExecutorService终止.
当我的线程即将被终止时,我想执行一些状态清理.实现这一目标的最佳方法是什么?ExecutorService不容易为线程的生命周期提供钩子.
我不想关闭我的ExecutorService - 对于在它们到来时运行任务非常有用.
ExecutorService executor = Executors.newCachedThreadPool(new MyThreadFactory());
// Do some work
executor.submit(new MyCallable());
// Need a way for the ExecutorService to notify me when it is about to
// terminate my thread - need to perform some cleanup
Run Code Online (Sandbox Code Playgroud)
谢谢,
Shreyas
我有一个ScheduledExecutorService任务计划在一小时内执行.如何获取未完成任务的列表,以便我可以强制它们立即运行?
我相信shutdown()会等待一个小时,它看起来好像shutdownNow()返回一个无法运行的Runnables列表(),因为Runnable实现检查Executor状态,当它注意到它已关闭Runnable拒绝运行时.请参阅ScheduledThreadPoolExecutor.ScheduledFutureTask.run()实际实施.
有任何想法吗?
final ExecutorService executor = Executors.newFixedThreadPool(1);
final Future<?> future = executor.submit(myRunnable);
executor.shutdown();
if(executor.awaitTermination(10, TimeUnit.SECONDS)) {
System.out.println("task completed");
}else{
System.out.println("Executor is shutdown now");
}
//MyRunnable method is defined as task which I want to execute in a different thread.
Run Code Online (Sandbox Code Playgroud)
这是run执行者类的方法:
public void run() {
try {
Thread.sleep(20 * 1000);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}}
Run Code Online (Sandbox Code Playgroud)
在这里它等待20第二,但是当我运行代码时它抛出一个异常:
java.lang.InterruptedException: sleep interrupted
at java.lang.Thread.sleep(Native Method)
Run Code Online (Sandbox Code Playgroud)
我无法关闭并发线程破坏Java Executor class.这是我的代码流程:
MyRunnableexecutor 等待10秒钟完成任务.我已经使用执行程序提交了一个任务,我需要它在一段时间后停止(例如5分钟).我试过这样做:
for (Future<?> fut : e.invokeAll(tasks, 300, TimeUnit.SECONDS)) {
try {
fut.get();
} catch (CancellationException ex) {
fut.cancel(true);
tasks.clear();
} catch(ExecutionException ex){
ex.printStackTrace(); //FIXME: gestita con printstack
}
}
Run Code Online (Sandbox Code Playgroud)
但我总是得到一个错误:我有一个需要被任务修改然后由线程读取的共享Vector,即使我停止所有任务,如果超时发生,我得到:
Exception in thread "Thread-1" java.util.ConcurrentModificationException
Run Code Online (Sandbox Code Playgroud)
有什么不对?如何停止提交的5分钟后仍在工作的任务?
我需要执行一些AsyncTasks然后收集所有结果,合并然后将最终结果返回给我的用户.
我正在寻找一种AsyncTask在Android中管理多个s 的方法.我正在考虑使用ExecutorServiceJava Concurrency包中的一个,但我卡住了因为ExecutorService只运行Runnables或Callables.为了确定我的要求,我可以使用
ExecutorService.invokeAll((Collection<? extends Callable<T>> tasks)
该invokeAll()方法List<Future><V>>仅在完成所有提交的任务时返回一个列表,并且我可以从相应的任务中获取每个任务的结果Future.
一切都很好,ExecutorService因为它不接受这一事实AsyncTask.
有没有其他方法可以使用AsyncTask,ExecutorService或者如果你可以请推荐一种不同的方法.
multithreading android asynchronous executorservice android-asynctask
我有一个fixedThreadPool,我用它来运行一堆工作线程来实现具有许多组件的任务的并行执行.
当所有线程都完成后,我使用方法(getResult)检索它们的结果(它们非常大)并将它们写入文件.
最终,为了节省内存并能够看到中间结果,我希望每个线程在完成执行后立即将其结果写入文件,然后释放其内存.
通常,我会在run()方法的末尾添加代码.但是,此类中的某些其他对象也会调用这些线程,但不要让它们将结果写入文件 - 而是使用其结果执行其他计算,最终将其写入文件.
所以,我想知道是否可以使用ExecutorService将回调函数附加到线程完成事件.这样,我可以立即检索其结果并释放该场景中的内存,但在其他场景中使用这些线程时不会破坏代码.
这样的事情可能吗?
我们假设我们有以下代码:
List<Future<?>> runningTasks;
ExecutorService executor;
...
void executeTask(Runnable task){
runningTasks.add(executor.submit(task));
}
Run Code Online (Sandbox Code Playgroud)
我的问题是:
runningTasks持有task对象的引用?该程序在九次打印后完成:
class BeeperControl {
private final ScheduledExecutorService scheduler = Executors.newScheduledThreadPool(1);
public void beep() {
final Runnable beeper = new Runnable() {
public void run() {
System.out.println("beep");
}
};
final ScheduledFuture<?> beeperHandle = scheduler.scheduleAtFixedRate(
beeper, 1, 1, SECONDS);
scheduler.schedule(new Runnable() {
public void run() {
beeperHandle.cancel(true);
}
}, 1 * 9, SECONDS);
}
public static void main(String[] args) {
BeeperControl bc = new BeeperControl();
bc.beep();
}
}
Run Code Online (Sandbox Code Playgroud)
如何停止进程(例如eclipse中的java进程),因为它在9秒内没有停止时间限制?
我定期运行任务并为间隔提供灵活性,下一个超时在每个任务结束时计算,从Instant.now()转换为毫秒,并使用调度ScheduledExecutorService#schedule.
这段代码通常工作正常(左边的蓝色曲线),但其他日子则不太好.
在我看来,事情有时会在启动时变坏(机器每晚都重新启动),虽然程序应该,并且确实纠正了自己ScheduledExecutorService#schedule不能恢复,并且计划任务一直运行得很晚.似乎完整的JVM重启是唯一的解决方案.
我最初的想法是,这是一个错误,根据机器启动的时间,事情可能会出错.但是以下日志输出表明该问题与我的用法有关ScheduledExecutorService#schedule:
// Log time in GMT+2, other times are in GMT
// The following lines are written following system startup (all times are correct)
08 juin 00:08:49.993 [main] WARN com.pgscada.webdyn.Webdyn - Scheduling next webdyn service time. Currently 2018-06-07T22:08:49.993Z, last connection null
08 juin 00:08:50.586 [main] INFO com.pgscada.webdyn.Webdyn - The next data sample at 2018-06-07T22:10:00Z and the next FTP connection at 2018-06-07T22:30:00Z
08 juin 00:08:50.586 [main] WARN com.pgscada.webdyn.Webdyn …Run Code Online (Sandbox Code Playgroud) executorservice ×10
java ×9
asynchronous ×2
future ×2
threadpool ×2
android ×1
callback ×1
runnable ×1
schedule ×1