executorservice

How to use invokeAll() to let all thread pool do their task?

谁都会走 提交于 2019-12-17 06:36:07
问题 ExecutorService pool=Executors.newFixedThreadPool(7); List<Future<Hotel>> future=new ArrayList<Future<Hotel>>(); List<Callable<Hotel>> callList = new ArrayList<Callable<Hotel>>(); for(int i=0;i<=diff;i++){ String str="2013-"+(liDates.get(i).get(Calendar.MONTH)+1)+"-"+liDates.get(i).get(Calendar.DATE); callList.add(new HotelCheapestFare(str)); } future=pool.invokeAll(callList); for(int i=0;i<=future.size();i++){ System.out.println("name is:"+future.get(i).get().getName()); } Now I want pool to

FixedThreadPool vs CachedThreadPool: the lesser of two evils

笑着哭i 提交于 2019-12-17 06:23:07
问题 I have a program that spawns threads (~5-150) which perform a bunch of tasks. Originally, I used a FixedThreadPool because this similar question suggested they were better suited for longer lived tasks and with my very limited knowledge of multithreading, I considered the average life of the threads (several minutes) " long lived ". However, I recently added the capability to spawn additional threads and doing so takes me above the thread limit I set. In this case, would it be better to guess

Impossible to make a cached thread pool with a size limit?

跟風遠走 提交于 2019-12-17 05:16:12
问题 It seems to be impossible to make a cached thread pool with a limit to the number of threads that it can create. Here is how static Executors.newCachedThreadPool is implemented in the standard Java library: public static ExecutorService newCachedThreadPool() { return new ThreadPoolExecutor(0, Integer.MAX_VALUE, 60L, TimeUnit.SECONDS, new SynchronousQueue<Runnable>()); } So, using that template to go on to create a fixed sized cached thread pool: new ThreadPoolExecutor(0, 3, 60L, TimeUnit

Choose between ExecutorService's submit and ExecutorService's execute

自作多情 提交于 2019-12-17 02:40:06
问题 How should I choose between ExecutorService's submit or execute, if the returned value is not my concern? If I test both, I didn't see any differences among the two except the returned value. ExecutorService threadExecutor = Executors.newSingleThreadExecutor(); threadExecutor.execute(new Task()); ExecutorService threadExecutor = Executors.newSingleThreadExecutor(); threadExecutor.submit(new Task()); 回答1: There is a difference concerning exception/error handling. A task queued with execute()

Shutdown or Not Shutdown? in ExecutorService (Java8)

梦想的初衷 提交于 2019-12-14 01:46:53
问题 I am trying to understand the behaviour of the executor service relative to shutdown. The documentation says that the application won't terminate unless there is a shutdown() call - but in this simple example. It exits after one minute precisely. Any idea? Runnable r = new Runnable() { @Override public void run() { Print.println("do nothing"); } }; ThreadFactory TF = (Runnable run) -> new Thread(run); ExecutorService exec = Executors.newCachedThreadPool(TF); exec.submit(r); returns this: 11

Running Multiple Thread Pools (ExecutorService) together

孤街浪徒 提交于 2019-12-13 09:32:51
问题 I have an object that I need to run through 4 scenarios. I want to split this between 2 threads (so I can send to an additional server) I got this working to the 2 servers, but in trying to clean up the code i have created what looks like this; ExecutorService executor1 = Executors.newFixedThreadPool(1); ExecutorService executor2 = Executors.newFixedThreadPool(1); executor1.execute(userProvisioner1); executor1.execute(userProvisioner2); executor2.execute(userProvisioner3); executor2.execute

Fibonacci on Java ExecutorService runs faster sequentially than in parallel

≯℡__Kan透↙ 提交于 2019-12-13 08:26:26
问题 I am trying out the executor service in Java, and wrote the following code to run Fibonacci (yes, the massively recursive version, just to stress out the executor service). Surprisingly, it will run faster if I set the nThreads to 1. It might be related to the fact that the size of each "task" submitted to the executor service is really small. But still it must be the same number also if I set nThreads to 1. To see if the access to the shared Atomic variables can cause this issue, I commented

Odd behavior with Runnable and ExecutorService

試著忘記壹切 提交于 2019-12-13 04:28:42
问题 I'm getting some really weird behavior with multithreading. I have two classes: DipoleTester and Dipole. DipoleTester attempts to create several Dipole objects, and then run them asynchronously. The problem is that DipoleTester just runs all of its Dipole objects at once, rather than 2 at a time. Here is DipoleTester: public class DipoleTester { public static String DIR = "./save/"; public static void main(String[] args) throws InterruptedException { Dipole trial; ExecutorService service =

Multi-threading : Multiple threads interacting with same table

前提是你 提交于 2019-12-13 02:42:28
问题 Interview question Say , we have a table with 2 million records in Employee table and we need to cut 10% salary(need to do some processing) of each employee and then save it back to collection. How can you do it efficiently. i asked him we can use executor framework for the same to create multiple threads which can fetch values from table then we can process and save it to list. then he asked me how will you check that a record is already processed or not, there i was clueless(how to do that)

ExecutorService - How to wait for completition of all tasks in non-blocking style

心已入冬 提交于 2019-12-13 01:34:49
问题 I am using ExecutorService in Java web server application for executing some computational tasks in parallel style and then calling shutdown() with awaitTermination() to wait for all tasks to be done. Whole computation can sometimes take dozens of minutes. The thing is awaitTermination() method blocks the main thread until timeout elapsed (or interrupted ) but I just want to start the tasks and immediatedly respond to client and after competition of all tasks shutdown the service ( following