concurrency

InterruptedException inside ExecutorService

自作多情 提交于 2020-08-27 21:31:02
问题 Should we set the interrupted flag when catching an InterruptedException inside a task managed by an ExecutorService ? Or should we just swallow the InterruptedException ? Example: final ExecutorService service = ...; final Object object = ...; service.submit(() -> { try { while (!condition) { object.wait(); } } catch (final InterruptedException exception) { Thread.currentThread().interrupt(); // yes or no? } }); 回答1: In a task submitted to an ExecutorService , receiving an interrupt is a

InterruptedException inside ExecutorService

自闭症网瘾萝莉.ら 提交于 2020-08-27 21:29:51
问题 Should we set the interrupted flag when catching an InterruptedException inside a task managed by an ExecutorService ? Or should we just swallow the InterruptedException ? Example: final ExecutorService service = ...; final Object object = ...; service.submit(() -> { try { while (!condition) { object.wait(); } } catch (final InterruptedException exception) { Thread.currentThread().interrupt(); // yes or no? } }); 回答1: In a task submitted to an ExecutorService , receiving an interrupt is a

InterruptedException inside ExecutorService

余生颓废 提交于 2020-08-27 21:28:09
问题 Should we set the interrupted flag when catching an InterruptedException inside a task managed by an ExecutorService ? Or should we just swallow the InterruptedException ? Example: final ExecutorService service = ...; final Object object = ...; service.submit(() -> { try { while (!condition) { object.wait(); } } catch (final InterruptedException exception) { Thread.currentThread().interrupt(); // yes or no? } }); 回答1: In a task submitted to an ExecutorService , receiving an interrupt is a

Python - Properly Kill/Exit Futures Thread?

对着背影说爱祢 提交于 2020-08-24 07:17:20
问题 I was previously using the threading.Thread module. Now I'm using concurrent.futures -> ThreadPoolExecutor . Previously, I was using the following code to exit/kill/finish a thread: def terminate_thread(thread): """Terminates a python thread from another thread. :param thread: a threading.Thread instance """ if not thread.isAlive(): return exc = ctypes.py_object(SystemExit) res = ctypes.pythonapi.PyThreadState_SetAsyncExc( ctypes.c_long(thread.ident), exc) if res == 0: raise ValueError(

If I use JSch from more than one thread, how should I use it

南笙酒味 提交于 2020-08-19 06:43:42
问题 Since connection creation takes up quite a few times, and I'd like to connect to multiple hosts, I started to use JSch from multiple threads. However I get some nasty exceptions, which I think is because of JSch being not thread-safe. How should I use it, that it not throws any exception, which is due to the not-thread-safety of JSch? Stacktrace: com.jcraft.jsch.JSchException: connection is closed by foreign host at com.jcraft.jsch.Session.connect(Session.java:269) at com.jcraft.jsch.Session