executorservice

How to stop next thread from running in a ScheduledThreadPoolExecutor

狂风中的少年 提交于 2019-11-29 17:36:21
I have a ScheduledThreadPoolExecutor which has one thread and runs for every 30 seconds. Now, if the current executing thread throws some exception, then I need to make sure that the next thread do not run and the the ScheduledThreadPoolExecutor is down. How do I achieve this? As a clean way, you can simply use a static accessed class to set/check the execution availability. import java.util.concurrent.atomic.AtomicBoolean; class ThreadManager { private static AtomicBoolean shouldStop = new AtomicBoolean(false); public static void setExceptionThrown(boolean val) { shouldStop.set(val); } public

How can I terminate Tasks that have timed out in multithreading?

醉酒当歌 提交于 2019-11-29 16:29:27
I need to make a library in which I will have synchronous and asynchronous methods in it. executeSynchronous() - waits until I have a result, returns the result. executeAsynchronous() - returns a Future immediately which can be processed after other things are done, if needed. Core Logic of my Library The customer will use our library and they will call it by passing DataKey builder object. We will then construct a URL by using that DataKey object and make a HTTP client call to that URL by executing it and after we get the response back as a JSON String, we will send that JSON String back to

How to stop a ScheduledExecutorService?

一曲冷凌霜 提交于 2019-11-29 12:31:04
问题 The program finishes after nine prints: class BeeperControl { private final ScheduledExecutorService scheduler = Executors.newScheduledThreadPool(1); public void beep() { final Runnable beeper = new Runnable() { public void run() { System.out.println("beep"); } }; final ScheduledFuture<?> beeperHandle = scheduler.scheduleAtFixedRate( beeper, 1, 1, SECONDS); scheduler.schedule(new Runnable() { public void run() { beeperHandle.cancel(true); } }, 1 * 9, SECONDS); } public static void main(String

How can I set Socket write timout in java?

≯℡__Kan透↙ 提交于 2019-11-29 11:59:59
I have a problem with handling socket in java. I am running a TCP server with multiple client connections. For performance reason, I used a simple thread pool to handle packets. Please see code below public enum LazyWorkCenter { instance; LazyWorkCenter() { lazyWorker = new NamedThreadPoolExecutor(3,3, 0L,TimeUnit.MILLISECONDS, "LazyWorker"); } private ExecutorService lazyWorker ; public void executeLazy(Runnable lazyTask) { lazyWorker.execute(lazyTask); } } public class TcpServerForClient { DataOutputStream out = null; DataInputStream in = null; public void onConnect(ServerSocket socket)

RejectedExecutionException inside single executor service

微笑、不失礼 提交于 2019-11-29 10:23:38
问题 In one of our services, someone added such (simplified) a piece of code: public class DeleteMe { public static void main(String[] args) { DeleteMe d = new DeleteMe(); for (int i = 0; i < 10_000; ++i) { d.trigger(i); } } private Future<?> trigger(int i) { ExecutorService es = Executors.newSingleThreadExecutor(); Future<?> f = es.submit(() -> { try { // some long running task Thread.sleep(10_000); } catch (InterruptedException e) { e.printStackTrace(); } }); return f; } } This fails sometimes

Waiting for all the threads to finish before shutting down the Executors

…衆ロ難τιáo~ 提交于 2019-11-29 08:11:26
Here is my code snippet. ExecutorService executor = Executors.newFixedThreadPool(ThreadPoolSize); while(conditionTrue) { ClassImplementingRunnable c = new ClassImplementingRunnable(); executor.submit(c); } Now after this is do executor.shutdown(); What i want to achieve here is that i want to wait for all the threads in the threadpool to have finished the execution and then i want to shutdown the executor. But i guess this is not what is happening here. The main thread seems to be executing shutdown and it just shuts down everything. Before when my threadpool size was 2, i did the following

Java: Force stopping of ExecutorService threads

帅比萌擦擦* 提交于 2019-11-29 07:35:47
My code: String[] torrentFiles = new File("/root/torrents/").list(); if(torrentFiles.length == 0 || torrentFiles == null) { System.exit(0); } ex = Executors.newFixedThreadPool(3); for(String torrentFile : torrentFiles) { ex.submit(new DownloadTorrent("/root/torrents/" + torrentFile)); } ex.shutdown(); try { ex.awaitTermination(30, TimeUnit.MINUTES); } catch(InterruptedException ex1) { Logger.getLogger(Main.class.getName()).log(Level.SEVERE, null, ex1); } But sometimes torrent downloading takes unknown time value and « awaitTermination » not works as I want. I need to stop all executed threads

converting to ScheduledThreadPoolExecutor

╄→гoц情女王★ 提交于 2019-11-29 03:57:24
问题 I am still a beginner at Java so I have not learned much about threads and concurrency. However, I would like to be able to use the ScheduledThreadPoolExecutor as a timer because of the problems I am having with java.util.Timer and TimerTask. I am extremely interested in the creation of threads and know that I will be learning about them in a few weeks. However, if possible could someone give me a basic example on how to convert my current mini test program using util.timer to using a

How many AsyncTasks i can run in an single process application

こ雲淡風輕ζ 提交于 2019-11-29 03:55:41
I am using asyncTasks, to load list elements with images (Just followed android's tutorial of efficiently loading bitmaps) In DDMS, i can see upto 5 AsyncTasks being running Now in addition i have added another AsyncTask, which performs some decoding using MediaCodec class. Now in DDMS, i still see 5 AsyncTasks, and my image loading aynctask or decoding async task executes, not both of them. When decoding is running, if i scroll the list, elements' images are not updated Counterly when i launch new decoding asynctask by calling it's execute method, the decoding doesn't start, but if i scroll

Thread.join() equivalent in executor

你说的曾经没有我的故事 提交于 2019-11-29 01:19:29
I have a newbie question. I have this code: public class Main { public static void main(String[] args) throws InterruptedException { // TODO Auto-generated method stub IntHolder aHolder=new IntHolder(); aHolder.Number=0; IncrementorThread A= new IncrementorThread(1, aHolder); IncrementorThread B= new IncrementorThread(2, aHolder); IncrementorThread C= new IncrementorThread(3, aHolder); A.start(); B.start(); C.start(); A.join(); B.join(); C.join(); System.out.println("All threads completed..."); } } Which will wait for all threads to complete. If I use Executors like this: public class Main {