Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- ExecutorService executor = Executors.newSingleThreadExecutor();
- try {
- //================= HERE ==================
- Future<Boolean> job = executor.submit(thirdPartyCallable);
- executor.awaitTermination(timeOut, TimeUnit.SECONDS);
- if(!job.isDone())
- logger.debug("Long call to 3rd party API didn't finish");
- //=========================================
- } catch (Exception exc) {
- exc.printStackTrace();
- } finally {
- if(!executor.isShutdown() )
- executor.shutdownNow();
- }
- }
- private static Callable<Boolean> thirdParytCallable = new Callable<Boolean>() {
- public Boolean call() throws Exception {
- //Call to long 3rd party API
- //.......
- for(long i = 0;i<99999991999999L;i++) {
- Thread.sleep(10L);// Emulates long call to 3rd party API
- System.out.print(".");
- }
- return Boolean.valueOf(true);//Data from 3rd party API
- }
- };
- ...
- workListExecutor.stop()
- ...
- boolean running = true;
- void stop(){
- running = false;
- }
- void run(){
- while (running) {
- ... do my stuff ...
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement