我需要提交一些任务,然后等待所有结果,直到所有结果都可用.它们中的每一个都添加了Stringa Vector(默认情况下是同步的).然后我需要为Vector中的每个结果启动一个新任务,但是只有当所有先前的任务都停止了它们的工作时我才需要这样做.
我想使用Java Executor,特别是我尝试使用Executors.newFixedThreadPool(100)以便使用固定数量的线程(我有一个可变数量的任务,可以是10或500)但我是执行者的新手,我不知道如何等待任务终止.这就像我的程序需要做的伪代码:
ExecutorService e = Executors.newFixedThreadPool(100);
while(true){
/*do something*/
for(...){
<start task>
}
<wait for all task termination>
for each String in result{
<start task>
}
<wait for all task termination>
}
Run Code Online (Sandbox Code Playgroud)
我不能做e.shutdown,因为我有一段时间(真的),我需要重用executorService...
你能帮助我吗?你能给我一个关于java执行器的指南/书吗?
我正在开发一个访问SQLite数据库的应用程序.问题是当有查询时数据库被锁定.大多数情况下,这不是问题,因为应用程序的流程非常线性.
但是我有一个很长的计算过程,由用户触发.此过程涉及在计算之间多次调用数据库.
我希望用户获得一些视觉反馈,因此我一直在使用Javafx progressIndicator和来自Javafx.Concurrency框架的服务.问题是这使用户可以自由地在应用程序中移动并可能触发对数据库的其他调用.
这导致数据库文件被锁定的异常.我希望有一种方法可以阻止该线程在这种情况发生时运行但是我无法在线找到任何明确的例子.他们中的大多数都过于简单了,我想要一种可扩展的方式.我尝试过使用cancel()方法,但这并不能保证线程会及时被取消.
因为我无法检查isCancelled的代码的所有部分,有时在线程被取消的时间和它有效停止的时间之间存在延迟.
所以我想到了以下解决方案,但我想知道在效率和避免竞争条件和悬挂方面是否有更好的方法.
// Start service
final CalculatorService calculatorService = new CalculatorService();
// Register service with thread manager
threadManager.registerService(CalculatorService);
// Show the progress indicator only when the service is running
progressIndicator.visibleProperty().bind(calculatorService.runningProperty());
calculatorService.setOnSucceeded(new EventHandler<WorkerStateEvent>() {
@Override
public void handle(WorkerStateEvent workerStateEvent) {
System.out.println("SUCCEEDED");
calculatorService.setStopped(true);
}
});
// If something goes wrong display message
calculatorService.setOnFailed(new EventHandler<WorkerStateEvent>() {
@Override
public void handle(WorkerStateEvent workerStateEvent) {
System.out.println("FAILED");
calculatorService.setStopped(true);
}
});
// Restart the service
calculatorService.restart();
Run Code Online (Sandbox Code Playgroud)
这是我的服务类,我已经将其子类化,包括可用于设置服务状态的方法(已停止或未停止)
public class CalculatorService extends Service implements …Run Code Online (Sandbox Code Playgroud) 我想用ScheduledExecutorService预定的方式导出一些数据。
在下面的代码中,我在 2 个不同的时间间隔内调用了 2 个不同的任务。ScheduledExecutorService当用户创建多个调度以导出多个数据(不同的报告)时,将有可能将多个任务安排在 1 中。
使用 singleScheduledExecutorService运行多个任务是否安全?
service.scheduleAtFixedRate(runnable2, 0, 10, TimeUnit.SECONDS);如果用户删除了特定的日程安排,是否可以停止其中一项任务(例如)?
public static void main(String... args) {
Runnable runnable = new Runnable() { public void run() {
// task to run goes here
System.out.println("Every 5 sec: "+ new java.util.Date());
}
};
Runnable runnable2 = new Runnable() {
public void run() {
// task to run goes here
System.out.println("Every 10 sec: "+ new java.util.Date());
}
};
ScheduledExecutorService service = Executors.newSingleThreadScheduledExecutor();
service.scheduleAtFixedRate(runnable, …Run Code Online (Sandbox Code Playgroud)我想取消正在运行的任务并将其替换为同一线程上的新任务。
在下面的代码中,我使用了一个单线程执行器来启动一个新任务并捕获一个Future代表它的。然后我用Future取消了任务。但是,任务并没有取消。
示例代码:
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.concurrent.Executors;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Future;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.SwingUtilities;
class Task implements Runnable {
public void run() {
System.out.println("New task started");
int i = 0; while (true) i++;
}
}
public class TaskLauncher extends JFrame {
private JButton button = new JButton("Start new task");
private ExecutorService executor = Executors.newSingleThreadExecutor();
private Future<?> future;
public TaskLauncher() {
button.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent event) …Run Code Online (Sandbox Code Playgroud)