executorservice

Waiting for threads to complete in a executor service

穿精又带淫゛_ 提交于 2019-12-02 14:30:59
问题 I have initialized a exectuor service with N threads. After the N threads finishes i want to wait for a while and then reuse the executor with new instance of N threads. How do i do this? Here is the sample code i am using that is failing: int NumberOfThreads=Integer.parseInt(PropertyHandler.getProperty("numberOfThreads")); ExecutorService executor = Executors.newFixedThreadPool(NumberOfThreads); log.info("Executor class has been initialized"); while (true) { jobStack = MrMestri.buildJobs();

What is the difference between ExecutorService.submit and ExecutorService.execute in this code in Java?

北城余情 提交于 2019-12-02 14:27:58
I am learning to use ExectorService to pool threads and send out tasks. I have a simple program below import java.util.concurrent.ExecutorService; import java.util.concurrent.Executors; import java.util.concurrent.TimeUnit; class Processor implements Runnable { private int id; public Processor(int id) { this.id = id; } public void run() { System.out.println("Starting: " + id); try { Thread.sleep(5000); } catch (InterruptedException e) { System.out.println("sorry, being interupted, good bye!"); System.out.println("Interrupted "+Thread.currentThread().getName()); e.printStackTrace(); } System

Issue when executing asynchronous tasks using ExecutorService

淺唱寂寞╮ 提交于 2019-12-02 10:08:02
I had asked a question earlier regarding ExecutorService and Apache Velocity initialization. To give a quick recap -- I have a Java EE frontend which accepts user requests and then for each of these requests, uses ExecutorService(SingleThreadedExecutor set as a daemon) to kick off a lengthy workflow.This workflow is contained in a library and works well and as expected when run in a standalone mode through eclipse. When called from the website(servlet) I observed that the workflows were consistently getting hung at the point where the Velocity Engine was being initialized (Velocity.init() or

Update JProgressBar from ExecutorService

心不动则不痛 提交于 2019-12-02 08:23:21
I am pinging gateways using Java ICMP ping function. To perform fast pinging I am using ExectorService which creates threads for pinging. After address is pinged (or not) I want to update Jprogressbar after pinging. I have this code which is working but it updates Jprogressbar before job (ping thread) is finished. I want to update jprogressbar after job is finished. private int NUM_THREADS = Runtime.getRuntime().availableProcessors(); ExecutorService exec = Executors.newFixedThreadPool(NUM_THREADS); public void run() { int JProgressBarValue = 0; for (;GateWayKey<=GateWayKeyStop;GateWayKey++){

making program to send mail by different threads at the same time through parallel processing

橙三吉。 提交于 2019-12-02 04:45:21
I have the below program which send the mail using java mail api , now this the is the simple program i have developed now i want to modify in terms of parallel execution by using executorframework that i want that 5 different threads independently should trigger my this program but those 5 different threads should trigger simultaneously at the same time lets say there are five different threads t1,t2,t3,t4 and t5 then all of them should independently hit my function which is main(@) is calling rite now but at the same time below is my java code public class SSendEmail { public static void

ExecutorService Future::get very slow

假如想象 提交于 2019-12-02 03:49:24
问题 I'm parallelizing a quite complex program to get it faster. For this I use most of the time the ExecutorService . Until now it worked pretty well, but then I noticed that just one line of code makes my program run half as fast as it could. It's the line with exactScore.get() . I don't know why, but it sometimes needs more that 0.1 s just to get the double value of the Future Object. Why is this? How can I handle it that it runs faster? Is there a way to write directly in the Double[] while

What is happening underneath the Future.cancel(true)

十年热恋 提交于 2019-12-02 00:06:18
Suppose I have a Runnable instance: class MyTask implements Runnable { public void run() { //some heavy calculation which takes time Thread.sleep(5000) //rest code ... } } Then, I use ExecutorService to submit the above task: ExecutorService service = Executors.newFixedThreadPool(3); Future<?> task = service.submit(new MyTask()); Now, I can cancel the task by task.cancel(true); . What I have understood is that the task.cancel(true) will interrupt the working thread in which this task is running, like Thread.currentThread().interrupt() . But this only sets a flag to tell that the working thread

Java ServiceExecutor terminating condition

家住魔仙堡 提交于 2019-12-01 23:33:56
I'm new to java executor stuff. I'm using Java's ExecutorService to launch several threads to process data. Executor executor = Executors.newFixedThreadPool(poolSize); for(int i=0; i< 5;i++) executor.execute(new MyRunnable(i)); once the threads don't find data, they gracefully terminate. My question is what happens to the Executor when all the threads terminate, is it still running its master thread ? or it will terminate itself and whole application will finish gracefully? in case executor thread still runs, how can I let it terminate once all its child threads are done (poolSize number of

ExecutorService Future::get very slow

纵然是瞬间 提交于 2019-12-01 23:32:52
I'm parallelizing a quite complex program to get it faster. For this I use most of the time the ExecutorService . Until now it worked pretty well, but then I noticed that just one line of code makes my program run half as fast as it could. It's the line with exactScore.get() . I don't know why, but it sometimes needs more that 0.1 s just to get the double value of the Future Object. Why is this? How can I handle it that it runs faster? Is there a way to write directly in the Double[] while multithreading? Thanks int processors = Runtime.getRuntime().availableProcessors(); ExecutorService

ExecutorService which runs tasks in calling thread?

ぐ巨炮叔叔 提交于 2019-12-01 20:56:24
Are there any java.util.ExecutorService implementations which simply run all executed tasks in the calling thread? If this isn't included in Java by default, is there a library which contains an implementation like this? The only existing implementation I could find is SynchronousExecutorService - unfortunately buried somewhere in camel library. Pasting source code (without comments) here for future reference: package org.apache.camel.util.concurrent; import java.util.List; import java.util.concurrent.AbstractExecutorService; import java.util.concurrent.TimeUnit; public class