栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

Java多线程学习笔记(二)

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

Java多线程学习笔记(二)

一、Future接口

java.util.concurrent包下,有以下方法:

cancel(boolean var1):取消Callable任务,未开始或已完成返回false,boolean参数表示是否中断执行中的Callable任务isCanceled():判断Callable任务是否取消了isDone():判断是否完成get():获得Callable的返回值get(long time, TimeUnit unit):设置超时时间,防止无限时间的等待 二、CompletionStage接口和CompletableFuture类

java.util.concurrent包下,jdk1.8才引入。用于阶段处理,实现链式的阶段型的操作,使线程与线程之间可以愉快的进行对话。目前只有CompletableFuture一个实现类。先看其中部分方法(3个一组,共15个方法):

1、方法后缀带Async,可以用线程池异步处理,默认线程池ForkJoinPool 1)、方法前缀带then,串行关系执行 √ color{#FF7D00}{√} √  Run方法,前一阶段结果不影响,无返回
public static void main(String[] args) throws ExecutionException, InterruptedException {
    System.out.println("main线程start");
    CompletableFuture future = CompletableFuture.supplyAsync(() -> {
        try {
            Thread.sleep(1000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        System.out.println("线程名称:" + Thread.currentThread().getName());
        return 1;
    }).thenRunAsync(() -> {
        System.out.println("线程名称:" + Thread.currentThread().getName());
    }, EXECUTOR_SERVICE);

    future.get();
    System.out.println("main线程end");
}

输出结果:

√ color{#FF7D00}{√} √  Accept方法,前一阶段结果变入参,无返回
public static void main(String[] args) throws ExecutionException, InterruptedException {
    System.out.println("main线程start");
    CompletableFuture future = CompletableFuture.supplyAsync(() -> {
        try {
            Thread.sleep(1000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        System.out.println("线程名称:" + Thread.currentThread().getName());
        return 1;
    }).thenAccept((t) -> {
        System.out.println("线程名称:" + Thread.currentThread().getName());
        System.out.println(t);
    });

    future.get();
    System.out.println("main线程end");
}

输出结果:

√ color{#FF7D00}{√} √  Apply方法,前一阶段结果变入参,有返回
public static void main(String[] args) throws ExecutionException, InterruptedException {
    System.out.println("main线程start");
    CompletableFuture future = CompletableFuture.supplyAsync(() -> {
        try {
            Thread.sleep(1000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        System.out.println("线程名称:" + Thread.currentThread().getName());
        return 1;
    }).thenApply((t) -> {
        System.out.println("线程名称:" + Thread.currentThread().getName());
        System.out.println(t);
        return t;
    });

    Integer t = future.get();
    System.out.println("返回结果:" + t);
    System.out.println("main线程end");
}

输出结果:

√ color{#FF7D00}{√} √  Combine方法,前一阶段 f1 和 f2 的结果变入参,有返回
public static void main(String[] args) throws ExecutionException, InterruptedException {
        System.out.println("main线程start");

        CompletableFuture f1 = CompletableFuture.supplyAsync(() -> {
            try {
                Thread.sleep(2000);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            System.out.println("f1线程名称:" + Thread.currentThread().getName());
            return 1;
        });

        CompletableFuture f2 = CompletableFuture.supplyAsync(() -> {
            try {
                Thread.sleep(1000);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            System.out.println("f2线程名称:" + Thread.currentThread().getName());
            return 2;
        }).thenCombine(f1, (t, u) -> {
            System.out.println("结合后线程名称:" + Thread.currentThread().getName());
            System.out.println("t:" + t);
            System.out.println("u:" + u);
            return t + u;
        });

        Integer result = f2.get();
        System.out.println("返回结果:" + result);
        System.out.println("main线程end");
    }

输出结果:

√ color{#FF7D00}{√} √  AcceptBoth方法,前一阶段 f1 和 f2 的结果变入参,无返回(和Combine方法一样,只是无返回)
转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/749330.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号