I am using ThreadPool through ExecutorService. By calling shutDownNow (), it interrupts all running threads in the pool. When this happens, I want these threads to give up their resources (socket connections and db) and just die, but no longer continue to work more logic, for example: inserting something into the database. What is the easiest way to achieve this? The following is sample code:
edit: forgot to mention that all my connections are released through finally. I just need to make sure that this achievement does not lead to the reliable use of various DB attachments.
finally
public void threadTest() { Thread t = new Thread(new Runnable() { public void run() { try { Thread.sleep(999999); } catch (InterruptedException e) { //invoke thread suicide logic here } } }); t.start(); t.interrupt(); try { Thread.sleep(4000); } catch (InterruptedException e) { } }
, , InterruptedException .
finally, , , . , , InterruptedException (.. throws ) voila!
throws
. - , throw InterruptedException, , . , , (.. / , ).
InterruptedException
, , " ", . , , , , . - ; , stop, .
stop
, , , , . finally, , , , - .
, t.interrupt() InterruptedException (, , -).
java.util.concurrent.locks.Lock( ) java.nio java.io
When the thread completes its start method, it will automatically die. So, actually do nothing:
public void threadTest() { Thread t = new Thread(new Runnable() { public void run() { try { Thread.sleep(999999); } catch (InterruptedException e) { //invoke thread suicide logic here // Do nothing so the thread will die } } }); t.start(); t.interrupt(); try { Thread.sleep(4000); } catch (InterruptedException e) { } }
The thread will be freed when threadTest () is completed because the thread variable will no longer be referenced.
Source: https://habr.com/ru/post/1741274/More articles:WPF: clicking and dragging a ListBox selects other items - wpfУдалить слой за кадром - iphoneДобавление существующего XML файла - c#Intent provided by cursor does not work correctly (LiveFolders) - androidajax response byte size - javascriptMono ripens for live websites? - .netLightweight plugin or procedure for sqlserver Management Studio before row insertion script - sql-serverConvert Word 2007 to Word 2003 programmatically in ASP.NET - asp.netHow to split a string into numeric and alphabetic characters using Regex - c #Access violation when running your own C ++ application that uses the built-in DLL - access-violationAll Articles