Advertisement
Guest User

Untitled

a guest
Aug 30th, 2014
187
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.04 KB | None | 0 0
  1. ExecutorService executor = Executors.newSingleThreadExecutor();
  2. try {
  3. //================= HERE ==================
  4. Future<Boolean> job = executor.submit(thirdPartyCallable);
  5. executor.awaitTermination(timeOut, TimeUnit.SECONDS);
  6. if(!job.isDone())
  7. logger.debug("Long call to 3rd party API didn't finish");
  8. //=========================================
  9. } catch (Exception exc) {
  10. exc.printStackTrace();
  11. } finally {
  12. if(!executor.isShutdown() )
  13. executor.shutdownNow();
  14. }
  15.  
  16. }
  17.  
  18. private static Callable<Boolean> thirdParytCallable = new Callable<Boolean>() {
  19. public Boolean call() throws Exception {
  20. //Call to long 3rd party API
  21. //.......
  22. for(long i = 0;i<99999991999999L;i++) {
  23. Thread.sleep(10L);// Emulates long call to 3rd party API
  24. System.out.print(".");
  25. }
  26. return Boolean.valueOf(true);//Data from 3rd party API
  27. }
  28. };
  29.  
  30. ...
  31. workListExecutor.stop()
  32. ...
  33.  
  34. boolean running = true;
  35.  
  36. void stop(){
  37. running = false;
  38. }
  39.  
  40. void run(){
  41. while (running) {
  42. ... do my stuff ...
  43. }
  44. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement