停止內部方法我創建了一個未來的任務,並提交給執行器。在run()方法中,我創建了一個placementPlanner對象。此類中的方法調用其他類。但是當我調用executor.shutDownNow()時,調用的內部方法不會停止執行。如何調用我殺死所有的子線程並停止調用方法。代碼如下。executor.shutDownNow()不從執行
static List<Result> invokePlacementPlannerWithTimeout(int timeoutSecs,
final String requestType, final RequestMap requestMap,
final ReleaseMap releaseMap, final SysConfig systemConfig,
final Request request, final SigmaBlade hostData,
final ServiceLevelData serviceLevelData,
final List<Service> serviceList, final String transactionID) throws IOException {
/**
* Object of ExecutorService interface used for timed execution of Placement
* Planner.
*/
ExecutorService executor = Executors.newFixedThreadPool(1);
/**
* Creating a final List of Result type.
*/
final List<Result> resultList = new ArrayList<Result>();
/**
* set the executor thread working
*/
Future<?> future = executor.submit(new Runnable() {
public void run() {
try {
Result result = new Result();
PlacementPlanner planner = new PlacementPlanner();
result.setRequestMapACK(planner.getRequestResult(
requestMap, systemConfig, request, hostData,
serviceLevelData, transactionID,
configFileLocation));
resultList.add(result);
} catch (Exception e) {
System.out.println("Exception in run method in invokePlacementPlannerWithTimeout method in Controller");
throw new RuntimeException(e);
}
}
});
try {
/**
* invoking the future task with specified timeout.
*/
// System.out.println(timeoutSecs);
future.get(timeoutSecs, TimeUnit.MILLISECONDS);
/**
* Shutting down the service after successful execution.
*/
executor.shutdownNow();
} catch (Exception e) {
/**
* Shutting down the service if timeout is occured.
*/
executor.shutdownNow();
/**
* Canceling the task for future use.
*/
future.cancel(true);
System.out.println("Timeout Occureed.");
Runtime.getRuntime().exec("taskkill /F /IM " + "QNSim.exe*");
}
return resultList;
}
(你應該做的是讓你的線程正確中斷。[這](http://www.ibm.com/developerworks/java/library/j-jtp05236/index.html)可能是如何的最佳來源妥善處理中斷。 –