• 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
  • 相关阅读:
    Java中的关键字super
    东八区先生的AI公司有多离谱?
    iOS上架流程
    十、AT24C02
    Git_02_查看本地未推送的commit记录
    享元模式 & 基于享元模式的对象池设计与开发应用(设计模式与开发实践 P12)
    【C++进阶(四)】STL大法--list深度剖析&list迭代器问题探讨
    公司刚来的00后真卷,上班还没2年,跳到我们公司起薪20k....
    Linux系统编程(一)——环境搭建
    【Linux】改变缓存路径、清理缓存
  • 原文地址:https://blog.csdn.net/s17856147699/article/details/126604667