Don't like ads? PRO users don't see any ads ;-)
Guest

Untitled

By: a guest on Jul 17th, 2012  |  syntax: None  |  size: 0.62 KB  |  hits: 14  |  expires: Never
download  |  raw  |  embed  |  report abuse  |  print
Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
  1. Future task of ExecutorService not truly cancelling
  2. ExecutorService taskExecutor = Executors.newCachedThreadPool();
  3. private static Map <String, Future<Object>> results = new HashMap <String, Future<Object>>();      
  4.  
  5. Future<Object> future = taskExecutor.submit(new MyProcessor(uid));
  6. results.put(uid, future);
  7.        
  8. public static synchronized boolean cancelThread(String uid) {
  9.     Future<Object> future = results.get(uid);
  10.     boolean success = false;
  11.     if (future != null) {
  12.         success = (future.isDone() ? true : future.cancel(true));
  13.         if (success)
  14.             results.remove(uid);
  15.     }
  16.     return success;    
  17. }