加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 编程开发 > Java > 正文

java 多线程-实现Callable接口

发布时间:2020-12-15 05:25:58 所属栏目:Java 来源:网络整理
导读:实现callable接口加泛型,即返回的值类型,默认为Object 重写的call方法可以有返回值,可以抛出异常 public class ThreadDownload implements CallableBoolean { public Boolean call() throws Exception { return true; }public static void main(String[]a
  • 实现callable接口加泛型,即返回的值类型,默认为Object
  • 重写的call方法可以有返回值,可以抛出异常

    public class ThreadDownload implements Callable<Boolean> {

    public Boolean call() throws Exception
    {

    return true;

    }public static void main(String[]args) throws InterruptedException,ExecutionException{ThreadDownload a=new ThreadDownload();ThreadDownload b=new ThreadDownload();ThreadDownload c=new ThreadDownload();//创建执行服务ExecutorService ser =Executors.newFixedThreadPool(3);//提交执行Future<Boolean>result1=ser.submit(a);Future<Boolean>result2=ser.submit(b);Future<Boolean>result3=ser.submit(c);//获取结果boolean r1=result1.get();boolean r2=result2.get();boolean r3=result3.get();//关闭服务:ser.shutdownNow();}}

  • (编辑:李大同)

    【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

      推荐文章
        热点阅读