• Callable接口(类似于Runnable)



    Callable接口(类似于Runnable)

    一、概念

    1、简介

    • Callable接口类似于Runnable
    • new Thread(new FutureTask(Callable)).start(); 开启线程
    • new Thread().start(); 启动线程

    2、API文档

    在这里插入图片描述
    在这里插入图片描述
    在这里插入图片描述

    3、源码

    在这里插入图片描述

    1. 可以有返回值
    2. 可以抛出异常
    3. 方法不同 call()

    二、代码案例

    1、案例1(1个线程)

    package com.sgz.callable;
    
    import java.util.concurrent.Callable;
    import java.util.concurrent.ExecutionException;
    import java.util.concurrent.FutureTask;
    
    /**
     * 日期:2022/8/29 - 11:30
     * 需求:
     */
    public class CallableTest {
        public static void main(String[] args) throws ExecutionException, InterruptedException {
            // new Thread(new Runnable()).start();
            // new Thread(new FutureTask()).start();
            // new Thread(new FutureTask(Callable)).start();
          //  new Thread().start();   // 怎么启动 Callable
    
            MyThread thread = new MyThread();
            FutureTask futureTask = new FutureTask(thread);     // 适配类
            new Thread(futureTask,"A").start();
    
            Integer o = (Integer) futureTask.get(); // 获取 Callable 的返回结果
            System.out.println(o);
        }
    }
    
    // 泛型的参数等于方法的返回值
    class MyThread implements Callable<Integer> {
    
        @Override
        public Integer call() {
            System.out.println("call()");
            return 1024;
        }
    }
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36

    细节:

    1. 有缓存
    2. 结果可能需要等待,会阻塞

    2、案例2(2个线程)

    package com.sgz.callable;
    
    import java.util.concurrent.Callable;
    import java.util.concurrent.ExecutionException;
    import java.util.concurrent.FutureTask;
    
    /**
     * 日期:2022/8/29 - 11:30
     * 需求:
     */
    public class CallableTest {
        public static void main(String[] args) throws ExecutionException, InterruptedException {
            // new Thread(new Runnable()).start();
            // new Thread(new FutureTask()).start();
            // new Thread(new FutureTask(Callable)).start();
          //  new Thread().start();   // 怎么启动 Callable
    
            MyThread thread = new MyThread();
            FutureTask futureTask = new FutureTask(thread);     // 适配类
            new Thread(futureTask,"A").start();
            new Thread(futureTask,"B").start(); // 结果会被缓存,效率高
    
            Integer o = (Integer) futureTask.get(); // 获取 Callable 的返回结果,get方法可能会产生阻塞,一般把它放到最好或者使用异步通信来处理
            System.out.println(o);
        }
    }
    
    // 泛型的参数等于方法的返回值
    class MyThread implements Callable<Integer> {
    
        @Override
        public Integer call() {
            System.out.println("call()");  
            // 耗时操作
            return 1024;
        }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
  • 相关阅读:
    Flowable钉钉对接005-完成钉钉任务
    无恒实验室联合GORM推出安全好用的ORM框架-GEN
    文字阴影(了解就行)
    postgres和postgis下载链接
    maptalks三维地图网址
    三、equals重写规范
    uniCloud 入门前端数据库
    Ajax中的JSONP
    【LeetCode每日一题合集】2023.9.11-2023.9.17(⭐反悔贪心&拓扑排序&Floyd)
    【C++】日期类实现,与日期计算相关OJ题
  • 原文地址:https://blog.csdn.net/s17856147699/article/details/126604667