zoukankan      html  css  js  c++  java
  • java 接口方法超时异常处理 设置超时时间

    原文:https://blog.csdn.net/coding_1994/article/details/87728374

    使用线程池另起一个线程,可以使用  newFixedThreadPool()  也可以使用  newSingleThreadExecutor();具体的这两个线程池如何使用自行百度或者去编程思想看看。

    public class InterfaceTimeOut{
        public static void main(String args[]){
            final ExecutorService exec = Executors.newSingleThreadExecutor();
            Callable<String>  call = new Callable<String>(){
                @Ovrride
                public String call() throws Exception{
                    Thread.sleep(1000 * 5);
                    return "线程执行完";
                }
            };
     
            Future<String> future = exec.submit(call);
            String obj = null;
            try{
                obj = future.get(1000 * 6,TimeUnit.MILLISECONDS);
                System.out.print("任务成功返回"+obj);
            }catch(InterruptedException e){
                e.printStackTrace();
            }catch(ExecutionException e){
                e.printStackTrace();
            }catch(TimeoutException e){
                e.printStackTrace();
            }
            exec.shutdown();
        }
    }
  • 相关阅读:
    Poj_1088_滑雪(DP)
    Poj_1088_滑雪(DP)
    Poj_1011_Sticks(剪枝)
    Poj_1011_Sticks(剪枝)
    Poj_1068 Parencodings
    Poj_1068 Parencodings
    Poj_1005_I Think I Need A HouseBoat
    Poj_1005_I Think I Need A HouseBoat
    Poj_1004_FinancialManagement
    git分支管理
  • 原文地址:https://www.cnblogs.com/shihaiming/p/10889329.html
Copyright © 2011-2022 走看看