前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >专栏 >同步代码实现

同步代码实现

作者头像
ma布
发布2025-06-02 08:29:28
发布2025-06-02 08:29:28
9800
代码可运行
举报
文章被收录于专栏:Java开发Java开发
运行总次数:0
代码可运行

一:三个线程顺序打印

1.使用join()方法

代码语言:javascript
代码运行次数:0
运行
复制
package tongbu;

public class JoinDemo {
    public static void main(String[] args) {
        Thread thread = new Thread(() -> {
            System.out.println("你好1");
        });

        Thread thread1 = new Thread(() -> {
            try {
                thread.join();
            } catch (InterruptedException e) {
                throw new RuntimeException(e);
            }
            System.out.println("你好2");
        });

        Thread thread2 = new Thread(() -> {
            try {
                thread.join();
            } catch (InterruptedException e) {
                throw new RuntimeException(e);
            }
            System.out.println("你好3");
        });

        thread.start();
        thread1.start();
        thread2.start();
    }
}

运行结果:

2.使用CountDownLatch

代码语言:javascript
代码运行次数:0
运行
复制
package tongbu;

import java.util.concurrent.CountDownLatch;

/**
 * @author: dlwlrma
 * @data 2025年05月31日 17:14
 * @Description
 */
public class CountDownLattchDemo {
    public static void main(String[] args) {
        CountDownLatch latch1 = new CountDownLatch(1);
        CountDownLatch latch2 = new CountDownLatch(1);

        Thread thread = new Thread(() -> {
            System.out.println("你好1");
            //释放
            latch1.countDown();
        });

        Thread thread1 = new Thread(() -> {
            try {
                latch1.await();
            } catch (InterruptedException e) {
                throw new RuntimeException(e);
            }
            System.out.println("你好2");
            latch2.countDown();
        });

        Thread thread2 = new Thread(() -> {
            try {
                latch2.await();
            } catch (InterruptedException e) {
                throw new RuntimeException(e);
            }
            System.out.println("你好3");
        });

        thread.start();
        thread1.start();
        thread2.start();
    }
}

运行结果同上

二:CompletableFuture

任务一执行完毕后之后执行任务二,任务三和任务一任务二一起执行,所有任务都有返回值,等任务二和任务三都执行完成后,在执行任务四

代码语言:javascript
代码运行次数:0
运行
复制
package yibu;

import java.util.concurrent.CompletableFuture;
import java.util.concurrent.ExecutionException;

/**
 * @author: dlwlrma
 * @data 2025年05月31日 18:26
 * @Description
 * 任务描述:任务一执行完毕后之后执行任务二,任务三和任务一任务二一起执行,所有任务都有返回值,等任务二和任务三都执行完成后,在执行任务四
 */
public class CompletableFutureDemo {
    public static void main(String[] args) {
        CompletableFuture<String> task1 = CompletableFuture.supplyAsync(()->{
           try {
               Thread.sleep(1000);
           } catch (InterruptedException e) {
               Thread.currentThread().interrupt();
               throw new RuntimeException(e);
           }
           return "任务一已经执行完毕";
        });

        CompletableFuture<String> task2 = task1.handle((res1,Throwable)->{
            try {
                Thread.sleep(1000);
            } catch (InterruptedException e) {
                Thread.currentThread().interrupt();
                throw new RuntimeException(e);
            }
            return "任务二已经执行完毕";
        });

        CompletableFuture<String> task3 = CompletableFuture.supplyAsync(()->{
            try {
                Thread.sleep(1000);
            } catch (InterruptedException e) {
                Thread.currentThread().interrupt();
                throw new RuntimeException(e);
            }
            return "任务三已经执行完毕";
        });

        //执行第四步
        CompletableFuture<String> task4 = CompletableFuture.allOf(task2,task3).handle((res2,Throwable)->{
            try {
                return "返回结果4" +task2.get() + task3.get();
            } catch (ExecutionException | InterruptedException e) {
                throw new RuntimeException(e);
            }
        });

        String s = task4.join();
        System.out.println(s);
    }
}
本文参与 腾讯云自媒体同步曝光计划,分享自作者个人站点/博客。
原始发表:2025-06-01,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • 一:三个线程顺序打印
    • 1.使用join()方法
    • 2.使用CountDownLatch
  • 二:CompletableFuture
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档