Today in the project development needs to use the implementation method plus time control, if the method executes too long to jump out of execution, nonsense does not say, directly on the code, with the thread pool with callable and the future way to execute the method of time-out blocking. I hope you guys correct me.
Enable thread pool final Executorservice exec = Executors.newfixedthreadpool (1); callable<map<string, string>> call = new callable<map<string, string>> () {public Map <string, String> call () throws Exception { map<string,string> excutemap = new hashmap<string,string& gt; (); Excutemap = Sendsshsys (Servicepc, arrport);//The corresponding business logic is executed here, note that the parameters used in the call are final return excutemap;} }; Future<map<string, string>> future = Exec.submit (call); try { //20 seconds timeout, here is the return value of the call is taken out, if the time is still not completed, return null map=future.get (timeunit.seconds); } catch (Interruptedexception e) { } catch (Executionexception e) { } catch (TimeoutException e) { } //The map can be processed below. If map is null it means that execution time is too long and blocked.
Java uses thread pool (Executorservice) To implement method timeout blocking with callable and future implementations