、私はmyServiceという中で、次のようなAsync
方法ました:ここ春の非同期タスク数の取得方法私の春のアプリケーションで
@Async("threadPoolTaskExecutor")
public void exportObject(String id) {
System.out.println("exporting " + id);
try {
Thread.sleep(5000);
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println("exported " + id);
}
は私が呼んでいる私のController
内部TaskExecutor
豆
@Bean(name = "threadPoolTaskExecutor")
public TaskExecutor getTaskExecutor() {
ThreadPoolTaskExecutor threadPoolTaskExecutor = new ThreadPoolTaskExecutor();
threadPoolTaskExecutor.setCorePoolSize(5);
threadPoolTaskExecutor.setMaxPoolSize(5);
return threadPoolTaskExecutor;
}
を作成しています方法ですが次のようなAsync
メソッド:
for(int i = 0; i < 100; i++){
myService.exportObject(i+"");
}
今私は取得したいキューに追加された保留中の非同期タスクの総数。どのように私はこれを得ることができますか?私はコントローラクラスで以下を試しましたが、TaskExecutorにはこの目的を果たすメソッドがありません。
@Autowire
private TaskExecutor taskExecutor;
public int getPendingTaskCount(){
// taskExecutor.getPendingTaskCount(); // ?
}
それはまったく可能だろうと思います。保留中のタスクをカウントすると、他のすべてのスレッドをブロックする必要があるため、何らかのタスクを盗み出さないようにするか、カウントしている間に他のタスクを追加したり、その番号の*弱い/近似*を提供します。おもしろい... – Eugene