multithreading - Kill or stop a endless/not-returning-value thread in Java -


i have function in java. returns value after completes task. however, in conditions returns nothing. create runnable , run function thread. however, because of not returning value, not finish although task. process stays alive because waits returning value. there way kill thread after triggered or after timeout? stop() or destroy() did not work. during debug, thread seen alive , want bi deleted/removed

runnable runnable = new runnable() {             @override             public void run() {                int stat = runmyfunction();             }         };         thread thread = new thread(runnable);         thread.start(); 

java not support killing thread via method on java.lang.thread.

stop() , destroy() promising @ first glance, have both been deprecated.

the documentation destroy states:

this method designed destroy thread without cleanup. however, method never implemented. if if implemented, deadlock-prone

and stop:

this method inherently unsafe. stopping thread thread.stop causes unlock of monitors has locked (as natural consequence of unchecked threaddeath exception propagating stack). if of objects protected these monitors in inconsistent state, damaged objects become visible other threads, potentially resulting in arbitrary behavior.

thus when documentation says 'deprecated', means broken , must never used!?! java api designers put lot of work backwards compatibility of apis, other languages have removed these methods sun decided keep them internal guides (rightly or wrongly) not permit removal of public api method.

so, question remains. how 1 thread exit thread? sadly 1 must go out of ones way poll exit variable. can custom variable, or can standard flag within java.lang.thread accessible via 'interrupted()'. advantage of using interrupted() other java apis such io support flag during otherwise blocking api calls , exit throwing interruptedexception. detection of calling interrupt() not immediate, sets flag , relies on thread poll variable @ point in future.

oracle offers tutorial on how code using interrupt here.


Comments

Popular posts from this blog

javascript - Jquery show_hide, what to add in order to make the page scroll to the bottom of the hidden field once button is clicked -

javascript - Highcharts multi-color line -

javascript - Enter key does not work in search box -