java - Terminate a thread which is running a native code -


in application have wrapper on native code, called via jni bridge. native code needs executed in separate thread (parallel processing). problem code "hangs" thread needs terminated "by force". unfortunately haven't found "delicate" method so: general advise tell code in thread exit gracefully, can't native code (which 3rd party code above).

i use java concurrent api task submission:

future<integer> processfuture = taskexecutor.submit(callable);  try {     result = processfuture.get(this.executiontimeout, timeunit.seconds).intvalue(); } catch (timeoutexception e) {     // how kill thread here?     throw new executiontimeoutexception("execution timed out (max " + this.executiontimeout / 60 + "min)"); } catch (...) {     ... exception handling other cases } 

future#cancel() interrupt thread, not terminate it. used following trick:

class destroyablecallable implements callable<integer> {      private thread  workerthread;      @override     public integer call() {         workerthread = thread.currentthread();          return integer.valueof(jnibridge.process(...));     }      public void stopworkerthread() {         if (workerthread != null) {             workerthread.stop();         }     } }  destroyablecallable callable = new destroyablecallable();  future<integer> processfuture = taskexecutor.submit(callable);  try {     result = processfuture.get(this.executiontimeout, timeunit.seconds).intvalue(); } catch (timeoutexception e) {     processfuture.cancel(true);     // dirty:     callable.stopworkerthread();      threadpooltaskexecutor threadpooltaskexecutor = (threadpooltaskexecutor) taskexecutor;      logger.debug("poolsize: " + threadpooltaskexecutor.getpoolsize() + ", maxpoolsize:"                     + threadpooltaskexecutor.getmaxpoolsize() + ", activecount:"                     + threadpooltaskexecutor.getactivecount());     }      throw new ...; } catch (...) {     ... exception handling other cases } 

the questions/problems code:

  • is in general right way so? other more elegant alternatives?
  • activecount on task executor not decreased, task executor still "thinks" thread running
  • i had add workerthread != null check stopworkerthread() method, variable turned out null on case. can't understand these cases...

notes:

  • native code not consume file descriptors (sockets). passed block of data , returned same way.
  • native code cpu-intensive. though guarantees terminate, may take long time.

bounty edit: approach/suggestion revisit native code clear, please not offer in reply. need pure-java solution / workaround.

java has pure options force thread termination. ancient , deprecated thread.stop() (afaik). , no option safe thread termination (what why .stop() deprecated, , allowed not implemented jvm implementors).

the reason threads inside app shares memory , resources -- so, if force termination of thread in arbitrary point, can't prove sure terminated thread not left shared memory/resources in inconsistent state. , can't (in general) suppose which resources (possibly) dirty ('cos dont know @ point thread stopped).

so, if want threads of app able interrupt, solution provide -- @ design phase -- notation of "savepoints" -- locations in target thread's code, guaranteed not mutate shared state, safe thread exit here. , thread.stop() javadocs telling you: way interrupt thread safely design thread's code can response kind of interrupt request. kind of flag, checked thread time time.

i've trying tell you: can't thing you're asked using java threading/concurrency. way may suggest (it given here) job in separate process. forcibly kill process more safe thread since 1) processes more separated each other , 2) os takes care many cleanups after process termination. killing process not safe, since there exists kind of resources (files, example) not cleared os default, in case seems safe.

so design small separate application (may in java -- if third-party lib not provide other bindings, or in shell-script) job make computation. start such process main app, give job, , start watchdog. watchdog detects timeout -- kills process forcibly.

this draft of solution. can implement kind of processes pool, if want improve performance (starting process may takes time), , on...


Comments

Popular posts from this blog

node.js - Using Node without global install -

How to access a php class file from PHPFox framework into javascript code written in simple HTML file? -

java - Null response to php query in android, even though php works properly -