zoukankan      html  css  js  c++  java
  • FutureTask的简单用法

    package com.fmp.orderManager.util;

    import java.util.Date;
    import java.util.concurrent.Callable;
    import java.util.concurrent.ExecutionException;
    import java.util.concurrent.ExecutorService;
    import java.util.concurrent.Executors;
    import java.util.concurrent.FutureTask;
    import java.util.concurrent.TimeUnit;
    import java.util.concurrent.TimeoutException;

    import com.fmp.orderManager.pojo.OrderManager;
    import com.fmp.util.Public;

    public class FutureTest {
    public static void main(String[] args) {
    ExecutorService executor = Executors.newSingleThreadExecutor();
    /*实体类*/
    final OrderManager ord1 = new OrderManager();
    ord1.setOrderNo("11111");
    ord1.setCreatTime(Public.DateTOString(new Date())); /**时间*/
    FutureTask<OrderManager> future = new FutureTask<OrderManager>(
    new Callable<OrderManager>() {

    @Override
    public OrderManager call() throws Exception {
    // TODO Auto-generated method stub
    Thread.sleep(30*1000);
    System.out.println("---------沉睡中------------");
    System.out.println(ord1.getCreatTime());
    return ord1;
    }
    });
    executor.execute(future);
    System.out.println("future==" + future);
    try {
    OrderManager result = future.get(30*1000, TimeUnit.MILLISECONDS);
    System.out.println(Public.DateTOString(new Date()));
    System.out.println("result==" + result.getOrderNo());
    } catch (InterruptedException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
    } catch (ExecutionException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
    } catch (TimeoutException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
    } finally {
    future.cancel(true);
    executor.shutdown();
    }
    }

    }

     

  • 相关阅读:
    css选择器的分类及优先级计算方法总结
    第四周实验总结&实验报告
    2019春季总结
    第七周课程总结&实验报告五
    第六周学习总结&java实验报告四
    第二周Java学习总结
    第九周课程总结&实验报告(七)
    第五周学习总结&实验报告三
    第三周课程总结及实验报告
    第八周课程总结&实验报告(六)
  • 原文地址:https://www.cnblogs.com/cai170221/p/9488041.html
Copyright © 2011-2022 走看看