java - Kill a task in threadexecutor if it takes more time -
here scenario:
i have java main process, uses jms publish activemq broker.
each time message supposed sent broker, thread used fixed sized thread pool (using threadexecutor) , inside publish calls made.
now , publish call blocking call , if broker down , thread keeps waiting.
i want create threadpool such if specific thread not on task in x amount of time, returns i.e if broker down , publish not go through, thread not keep waiting , instead return pool.
there no way of make publish call asynchronous, way handle situation 1 mentioned above guess.
are there threadexecutors out there allow me terminate thread immidiately, if thread not able complete task in given timeframe??
would love more elegant solution someone.
the executorservice
interface offers methods invokeall()
, invokeany()
timeout
parameter.
example:
executorservice executor = executors..newfixedthreadpool(2); executor.invokeall(arrays.aslist(new callable<void>(){ @override public void call() throws exception { try { // task } catch (interruptedexception e) { // task forced end } return null; } }), 5, timeunit.seconds); executor.shutdown();
Comments
Post a Comment