executorservice

ScheduledExecutorService and ThreadPoolTaskExecutor that interrupts tasks after a timeout

元气小坏坏 提交于 2020-01-17 03:44:14
问题 I Used ExecutorService that interrupts tasks after a timeout.I use a ScheduledExecutorService for this. First I submitted the thread and it once to begin immediately and retain the future that is created. After that i use ScheduledExecutorService as a new task that would cancel the retained future after some period of time. //Start Spring executor to submit tasks ThreadPoolTaskExecutor taskExecutor = (ThreadPoolTaskExecutor) ApplicationContextProvider.getApplicationContext().getBean(

Why is java ExecutorService newSingleThreadExecutor spawning two threads?

淺唱寂寞╮ 提交于 2020-01-14 07:37:07
问题 I have a sample java code below which if run as a console application behaves as I expected ( spawning a single thread to execute the runnable). The strange behavior (spawning two threads - sample below) I see is when I run this sample as a service application using Apache's prunsrv64.exe. I am testing this on a Windows 7 machine - 64bit. Sample Output: Thread -28 Current time: 09:50:11 AM Thread -52 Current time: 09:50:12 AM Thread -28 Current time: 09:50:21 AM Thread -52 Current time: 09:50

Can't stop a task which is started using ExecutorService

家住魔仙堡 提交于 2020-01-13 18:06:04
问题 Sorry I have to open a new thread to describe this problem. This morning I asked this question, there're some replies but my problem is still not solved. This time I will attach some runnable code(simplified but with the same problem) for you to reproduce the problem: public class ThreadPoolTest { public static void main(String[] args) throws Exception { final ExecutorService taskExecutor = Executors.newFixedThreadPool(5); Future<Void> futures[] = new Future[5]; for (int i = 0; i < futures

Program does not terminate immediately when all ExecutorService tasks are done

一世执手 提交于 2020-01-10 09:33:08
问题 I put a bunch of runnable objects into an ExecutorService: // simplified content of main method ExecutorService threadPool = Executors.newCachedThreadPool(); for(int i = 0; i < workerCount; i++) { threadPool.execute(new Worker()); } I would expect my program/process to stop immediately after all workers are done. But according to my log, it takes another 20-30 seconds until that happens. The workers do not allocate any resources, in fact, they do nothing at the moment. Don't get me wrong,

JVM does not exit when TimeoutException occurs

ⅰ亾dé卋堺 提交于 2020-01-06 12:51:09
问题 I have code which needs to do something like this There is a list of classes each with some method (lets say execute()). I need to invoke that method on each class and there is a fixed timeOut for each invocation. Now, one of the class's execute method is badly written and results in a timeout due to which the jvm does not exit. I am running the class like this. java ExecutorServiceTest execute TestClass1 TestClass2 TestClass3 Why does the jvm not exit after completing the execution of the

Performing a long calculation that returns after a timeout

半城伤御伤魂 提交于 2020-01-06 01:06:07
问题 I want to perform a search using iterative deepening, meaning every time I do it, I go deeper and it takes longer. There is a time limit (2 seconds) to get the best result possible. From what I've researched, the best way to do this is using an ExecutorService, a Future and interrupting it when the time runs out. This is what I have at the moment: In my main function: ExecutorService service = Executors.newSingleThreadExecutor(); ab = new AB(); Future<Integer> f = service.submit(ab); Integer

Calling ExecutorService after some interval of time

无人久伴 提交于 2020-01-05 03:01:07
问题 Is their a way to schedule a thread pool using ExecutorService , in lines similar to thread.sleep() My current code looks something like Executors.newScheduledThreadPool(poolSize); public void run() { try { pool.execute(new Worker()); } But I want to call the run method, only after some time interval. Can someone let me know how to do this? 回答1: This can be achieved using ScheduledThreadPoolExecutor. Sample code pool = new ScheduledThreadPoolExecutor(10); pool.scheduleWithFixedDelay(new

How to run two classes in parallel using multithreading?

时光怂恿深爱的人放手 提交于 2020-01-03 15:34:38
问题 I am working on a project in which I have multiple interface and two Implementations classes which needs to implement these two interfaces. Suppose my first Interface is - public Interface interfaceA { public String abc() throws Exception; } And its implementation is - public class TestA implements interfaceA { // abc method } I am calling it like this - TestA testA = new TestA(); testA.abc(); Now my second interface is - public Interface interfaceB { public String xyz() throws Exception; }

Tomcat 7 and ScheduledExecutorService.shutdown

你。 提交于 2020-01-03 11:18:05
问题 I am using ScheduledExecutorService to run scheduled threads. I implemented ServletContextListener.contextDestroyed and invoked ScheduledExecutorService.shutdownNow and awaitTermination . Here is an example: @Override public void contextDestroyed(ServletContextEvent servletcontextevent) { pool.shutdownNow(); // Disable new tasks from being submitted try { // Wait a while for existing tasks to terminate if (!pool.awaitTermination(50, TimeUnit.SECONDS)) { pool.shutdownNow(); // Cancel currently

Deterministic assignment of tasks to threads using ExecutorService

孤者浪人 提交于 2020-01-02 06:07:13
问题 Given Executor service with a fixed pool of threads, is it possible to guarantee deterministic assignment of tasks to threads? More precisely, assume there are just two threads, namely pool-thread-0 and pool-thread-1 and there is a collection of 2 tasks to be executed. What I wish to achieve is that the former thread always executes the first one, while the latter handles the remaining one. Here is an example: public static void main(String[] args) throws InterruptedException,