executorservice

Java ExecutorService - scaling

隐身守侯 提交于 2019-12-11 10:47:37
问题 I am trying to write a program in Java using ExecutorService and its function invokeAll . My question is: does the invokeAll function solve the tasks simultaneously? I mean, if I have two processors, there will be two workers at the same time? Because aI can't make it scale correctly. It takes the same time to complete the problem if I give newFixedThreadPool(2) or 1. List<Future<PartialSolution>> list = new ArrayList<Future<PartialSolution>>(); Collection<Callable<PartialSolution>> tasks =

Add executors dynamically based on a condition in java

风格不统一 提交于 2019-12-11 10:44:20
问题 If StrTemp.equals(true) , I want code as below (I have 2 threads here): ExecutorService executor = Executors.newFixedThreadPool(2); Future<String> f1 = executor.submit(new Callable<String >() { public String call() throws Exception { return dao.getS4PricingResponse(pricingRequestTemp); } }); Future<String> f2 = executor.submit(new Callable<String>() { public String call() throws Exception { return dao.getS4ProductResponse(productRequestTemp); } }); If it's not true I want three threads to be

Shutdown ExecutorService when no task has been submitted for a given time

瘦欲@ 提交于 2019-12-11 10:25:17
问题 I'm using an ExecutorService to call a service which basically connects to an application (local or remote via SSH), and allows to send commands to it and get its output. So, the Thread created by the ExecutorService is waiting for user input, and this input is then processed as a task through an implementation of the call method, which returns the output and looks like this: @Override public String call() throws Exception { write(command); return readResult(); } I would like to stop the

Why ThreadPoolExecutor finalize invokes shutdown and not shutdownNow

依然范特西╮ 提交于 2019-12-11 07:38:14
问题 Finalize method in class ThreadPoolExecutor Looks like below. protected void finalize() { shutdown(); } Consider below program in which thread never terminates ExecutorService executorService = Executors.newSingleThreadExecutor(); executorService.submit(new Runnable() { @Override public void run() { try { new SynchronousQueue<String>() .put("will never get removed"); } catch (InterruptedException e) { e.printStackTrace(); } } }); executorService = null; System.gc(); } In such case shutdownNow

Execute SELECT sql by randomly picking tables

淺唱寂寞╮ 提交于 2019-12-11 04:14:37
问题 I am working on a project in which I have two tables in a different database with different schemas. So that means I have two different connection parameters for those two tables to connect using JDBC- Let's suppose below is the config.property file- TABLES: table1 table2 #For Table1 table1.url: jdbc:mysql://localhost:3306/garden table1.user: gardener table1.password: shavel table1.driver: jdbc-driver table1.percentage: 80 #For Table2 table2.url: jdbc:mysql://otherhost:3306/forest table2.user

Using ExecutorService to repeatedly perform a number of similar tasks in parallel

ⅰ亾dé卋堺 提交于 2019-12-11 04:04:14
问题 There is Java code which when simplified looks like this: while(someCondition) { SomeType a = CalcResult(param1); SomeType b = CalcResult(param2); SomeType c = CalcResult(param3); // Do something with a, b and c } CalcResult() is time consuming. The application runs on an SMP system. There is a push to try running all three calculations simultaneously each on its own CPU instead of running them sequentially. It is always those 3 tasks that need to be parallel, not an arbitrary number of tasks

Set timeout on user input

℡╲_俬逩灬. 提交于 2019-12-11 02:18:40
问题 I'm trying to set a timeout on user input in a java console application with the following code ExecutorService executor = Executors.newSingleThreadExecutor(); Callable task =() -> { System.out.print("input: "); BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); return br.readLine(); }; Future future = executor.submit(task); String input = null; try { input = (String)future.get(10, TimeUnit.SECONDS); } catch (TimeoutException e) { future.cancel(true); System.out.println

Threadsafe static initialization of ExecutorService

主宰稳场 提交于 2019-12-11 01:18:28
问题 I am trying to create a threadsafe singleton class based on Initialization-on-demand holder idiom . Here is my code public class Check{ private Check(){ } private static class Provider { static final ExecutorService INSTANCE = new ThreadPoolExecutor(5, "read this val from file", 60L, TimeUnit.SECONDS, new LinkedBlockingQueue()); } public static ExecutorService getInstance() { return Provider.INSTANCE; } } My expectation is to initialize ExecutorService in a threadsafe manner and only one

How to reduce the time delay to reach run method of Runnable class using ExecutorService Java

好久不见. 提交于 2019-12-10 22:16:34
问题 I was trying to implement a real-time executing application in which a button click event will assign a task to a Thread , which will call a midi Method to play some music. The music has to be started immediately when button is clicked with a small delay. The midi codes are implemented in the run method of Runnable class. But to reach the 1st statement of run method after the button click event itself is taking more than 2 milli second . I tried to use Executor Service, since it can have

Why am I getting a NullPointerException when fetching values from Futures?

霸气de小男生 提交于 2019-12-10 21:07:16
问题 I'm using and ExecutorService to concurrently run some Callables . Here's a simplified version of my code: ArrayList<Book> objResults = new ArrayList<Book>(); ExecutorService esrExecutor = Executors.newFixedThreadPool(2); Set<Callable<ArrayList<Book>>> setCallables = new HashSet<Callable<ArrayList<Book>>>(); setCallables.add(new Callable<ArrayList<Book>>() { public ArrayList<Book> call() throws Exception { ArrayList<Book> objAmazonResults = new ArrayList<Book>(); try { Amazon objAmazon = new