我希望我能写下类/接口名称,但我没有......
在查看JDK javadoc时,我看到了一个类/接口的引用,目的是收集和使用ExecutorService(完成的Futures<T>s)产生的结果,可能是系统中的其他地方.当时我记下了它,因为它非常适合我需要的东西,但我似乎无法从舌尖上得到这个类的名字.
任何人都知道我指的是什么?
可能重复:
ExecutorService,如何等待所有任务完成
Java ExecutorService:awaitTermination所有递归创建的任务
有没有办法阻止当前线程,直到ExecutorService完成所有任务?
executor.execute(task1);
executor.execute(task2);
executor.execute(task3);
executor.execute(task4);
executor.execute(task5);
// ...now I want to block until all tasks have finished executing...
System.out.println("done!")
Run Code Online (Sandbox Code Playgroud) 我的services.xml中有以下代码
<bean id="executorService" class="java.util.concurrent.Executors" factory-method="newFixedThreadPool">
<constructor-arg value="10" />
</bean>
<task:annotation-driven executor="executor" />
<task:executor id="executor" pool-size="10" queue-capacity="100" rejection-policy="CALLER_RUNS" />
Run Code Online (Sandbox Code Playgroud)
在同一项目中,我还具有使用dbcp.BasicDataSource的数据库连接。
我已经读到DBCP在您的应用程序是单线程而不是多线程时才有效。使用executor告诉我该应用程序是多线程的。您认为在这里使用DBCP是否不合适?这是一个好习惯吗?或者,我是否有一个古老的神话,那就是DBCP无法处理多种环境?
朝着正确方向的任何指导将不胜感激。
java spring multithreading executorservice apache-commons-dbcp
我正在使用expiry. 我正在使用 aScheduledThreadExecutor来安排从缓存中删除条目。我的问题是执行程序永远不会关闭。我已经尝试过executor.shutdown()方法,shutdownHook但即使在我的主程序完成执行之后它也没有被执行。我也不喜欢终结器。我的代码如下。我希望在closeCache()主程序退出时执行该方法。
public class TimeCacheManual<K,V> {
private final int maxSize;
private final long timeToLive;
private Map<K, V> keyValueMap;
private Map<K,ScheduledFuture > keySchedulerMap;
private Queue<K> keys;
private final ScheduledExecutorService scheduler;
/*
* creates new instance of TimeBasedEvictionCache.
* @param maxSize must be greater than zero
* @param timeToLive must be greater than zero
* @throws IllegalArgumentException if {@code maxSize<1||timeToLive<1}
* */
public TimeCacheManual(int maxSize,long timeToLive) {
if(maxSize<1||timeToLive<1){
throw new IllegalArgumentException(); …Run Code Online (Sandbox Code Playgroud) 我正在制作一些虚拟程序来学习这个java类.我的定时任务调用一个任务,在中断它之前不会给它3秒的时间.这是代码:
FutureTask<Integer> task = new FutureTask<>(new
Callable<Integer>(){
@Override
public Integer call() throws Exception {
int i =0;
while(i<100000){
;
}
return 0;
}
});
executor.execute(task);
try {
task.get(3000, TimeUnit.MILLISECONDS);
System.out.println("Everything was ok");
} catch (InterruptedException | ExecutionException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (TimeoutException ex){
boolean result = task.cancel(true); //here i cancel the thread
System.out.println("the task has timed out "+result);
}
Run Code Online (Sandbox Code Playgroud)
会发生什么是catch块被执行,但我的程序一直运行直到线程完成.这就像task.cancel不被接受.这是为什么?
我正在使用Executor Service创建3个线程(扩展Runnable)并提交它们,从我的Main类执行三个任务.如下所示:
ExecutorService executor = Executors
.newFixedThreadPool(3);
A a= new A();
B b= new B();
C c= new C();
/**
* Submit/Execute the jobs
*/
executor.execute(a);
executor.execute(b);
executor.execute(c);
try {
latch.await();
} catch (InterruptedException e) {
//handle - show info
executor.shutdownNow();
}
Run Code Online (Sandbox Code Playgroud)
当线程中发生异常时,我抓住它并执行System.exit(-1).但是,如果发生任何异常,我需要返回主类并在那里执行一些语句.这该怎么做?没有FutureTask,我们可以从这些线程返回一些东西吗?
我使用ExecutorService的Java的Web服务器应用程序的并行执行风格的一些计算任务,然后调用shutdown()与awaitTermination()等待做的所有任务.整个计算有时可能需要几十分钟.
awaitTermination()方法是阻塞主线程,直到超时(或中断),但我只是想启动任务并立即响应客户端,并在所有任务竞争后关闭服务(遵循约定总是关闭线程池).
所以我的问题是,有什么方法可以在完成所有任务后通知我,以便我可以调用该shutdown()方法?听者还是什么..
谢谢!
我试图执行两个方法,其中两个方法将在一段时间后逐个执行,我正在使用ExecuterService.我已经实现了部分代码但是到目前为止我无法实现的全部功能,在这里我发布了我的代码
public class ExampleExecuterService {
private static final int MYTHREADS = 3000;
public static void main(String[] args) throws Exception {
ExecutorService executor = Executors.newFixedThreadPool(MYTHREADS);
Object methodList[]={
aMethod(),bMethod()
};
for(int i=0;i<methodList.length;i++){
Object myList = methodList[i];
Runnable worker = new MyRunnable(myList);
executor.execute(worker);
}
executor.shutdown();
// Wait until all threads are finish
while (!executor.isTerminated()) {
}
System.out.println("\nFinished all threads");
}
public static class MyRunnable implements Runnable {
private Object myList=null;
MyRunnable(Object myList) {
this.myList = myList;
}
@Override
public void run() …Run Code Online (Sandbox Code Playgroud) import java.util.concurrent.Callable;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.Future;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.TimeoutException;
public class ExecutorServiceTest {
public static void main(String args[]) {
new ExecutorServiceTest();
}
public ExecutorServiceTest() {
while (true) {
action();
}
}
public String action() {
String string = "";
ExecutorService executor = Executors.newSingleThreadExecutor();
Future<String> future = executor.submit(new Callable() {
@Override
public String call() {
return randomString();
}
});
try {
string = future.get(1, TimeUnit.MILLISECONDS);
} catch (InterruptedException | ExecutionException | TimeoutException ex) {
}
future.cancel(true); …Run Code Online (Sandbox Code Playgroud) 我有一个类,它实现了Runnable接口,并且是一个一旦启动就会无限期运行的任务(一个长时间运行的线程)。
public class LongRunningTask implements Runnable {
@Override
public void run() {
//stuff happening here
}
}
Run Code Online (Sandbox Code Playgroud)
一个简单的ExecutorService/ThreadPoolExecutor创作:
final ExecutorService executorService = Executors.newFixedThreadPool(8);
Run Code Online (Sandbox Code Playgroud)
如果LongRunningTask实际启动/执行,我能够观察到它的实际结果,因此,我注意到了这一点:
如果我传递给它执行executorService.execute(() -> new LongRunningTask());,它根本不会执行,也不会有结果。
如果我通过它来执行executorService.execute(new LongRunningTask());它,它将按照它应该的方式执行并且会有结果。
使用 的 lambda 语法有() ->什么区别?