前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >java8并行计算

java8并行计算

作者头像
oktokeep
发布2024-10-09 12:34:22
960
发布2024-10-09 12:34:22
举报
文章被收录于专栏:第三方工具

1.串行计算的模拟运行时间(time=532)

2.CompleteFuture并行计算(time=231)

3.Callable并行计算 (time=208)

Java8 多线程及并行计算demo

* https://cloud.tencent.com/developer/article/2455663

需求背景:比如一个大的对象(userInfo),包含3个部分的集合等数据的计算,查询等。可以启动3个多线程来并行计算。最后计算完毕之后,组装对象,并行计算完毕。

1.串行计算的模拟运行时间

代码语言:javascript
复制
package com.example.core.mydemo.bean;

import com.example.core.mydemo.json2.GsonUtils;

import java.util.concurrent.CompletableFuture;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

/**
 * userInfo={"username":"刘德华","sex":"男","age":30},time=532
 */
public class CompleteFutureBeforeTest {

    public static void main(String[] args) throws Exception{
        long now = System.currentTimeMillis();
        final UserInfo userInfo = new UserInfo();
        getRemoteUserAndFill(1, userInfo);

        getRemoteBonusAndFill(1, userInfo);

        getRemoteGrowthAndFill(1, userInfo);
        long end = System.currentTimeMillis();
        System.out.println("userInfo=" + GsonUtils.toJson(userInfo) + ",time=" + (end-now));
    }

    private static void getRemoteUserAndFill(int i, UserInfo userInfo) throws Exception{
        userInfo.setUsername("刘德华");
        Thread.sleep(200);
    }
    private static void getRemoteBonusAndFill(int i, UserInfo userInfo) throws Exception{
        userInfo.setSex("男");
        Thread.sleep(150);
    }
    private static void getRemoteGrowthAndFill(int i, UserInfo userInfo) throws Exception{
        userInfo.setAge(30);
        Thread.sleep(180);
    }

}

2.CompleteFuture并行计算

代码语言:javascript
复制
package com.example.core.mydemo.bean;

import com.example.core.mydemo.json2.GsonUtils;

import java.util.concurrent.CompletableFuture;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

/**
 * userInfo={"username":"刘德华","sex":"男","age":30},time=231
 *
 * Java8 多线程及并行计算demo
 * https://www.cnblogs.com/oktokeep/p/16639417.html
 */
public class CompleteFutureTest {

    public static void main(String[] args) throws Exception{
        long now = System.currentTimeMillis();

        ExecutorService executorService = Executors.newFixedThreadPool(10);

        final UserInfo userInfo = new UserInfo();
        CompletableFuture userFuture = CompletableFuture.supplyAsync(() -> {
            try {
                getRemoteUserAndFill(1, userInfo);
            } catch (Exception e) {
                e.printStackTrace();
            }
            return Boolean.TRUE;
        }, executorService);

        CompletableFuture bonusFuture = CompletableFuture.supplyAsync(() -> {
            try {
                getRemoteBonusAndFill(1, userInfo);
            } catch (Exception e) {
                e.printStackTrace();
            }
            return Boolean.TRUE;
        }, executorService);

        CompletableFuture growthFuture = CompletableFuture.supplyAsync(() -> {
            try {
                getRemoteGrowthAndFill(1, userInfo);
            } catch (Exception e) {
                e.printStackTrace();
            }
            return Boolean.TRUE;
        }, executorService);
        CompletableFuture.allOf(userFuture, bonusFuture, growthFuture).join();

        userFuture.get();
        bonusFuture.get();
        growthFuture.get();

        long end = System.currentTimeMillis();
        System.out.println("userInfo=" + GsonUtils.toJson(userInfo) + ",time=" + (end-now));
    }

    private static void getRemoteUserAndFill(int i, UserInfo userInfo) throws Exception{
        userInfo.setUsername("刘德华");
        Thread.sleep(200);
    }
    private static void getRemoteBonusAndFill(int i, UserInfo userInfo) throws Exception{
        userInfo.setSex("男");
        Thread.sleep(150);
    }
    private static void getRemoteGrowthAndFill(int i, UserInfo userInfo) throws Exception{
        userInfo.setAge(30);
        Thread.sleep(180);
    }
}

3.Callable并行计算

代码语言:javascript
复制
package com.example.core.mydemo.bean.callable;

import com.example.core.mydemo.bean.UserInfo;

import java.util.concurrent.Callable;

public class UserAndFill implements Callable<UserInfo> {
    UserInfo userInfo;

    public UserAndFill(UserInfo userInfo) {
        this.userInfo = userInfo;
    }

    @Override
    public UserInfo call() throws Exception {
        userInfo.setUsername("刘德华");
        Thread.sleep(200);
        return userInfo;
    }
}
代码语言:javascript
复制
package com.example.core.mydemo.bean.callable;

import com.example.core.mydemo.bean.UserInfo;

import java.util.concurrent.Callable;

public class BonusAndFill implements Callable<UserInfo> {
    UserInfo userInfo;

    public BonusAndFill(UserInfo userInfo) {
        this.userInfo = userInfo;
    }

    @Override
    public UserInfo call() throws Exception {
        userInfo.setSex("男");
        Thread.sleep(150);
        return userInfo;
    }
}
代码语言:javascript
复制
package com.example.core.mydemo.bean.callable;

import com.example.core.mydemo.bean.UserInfo;

import java.util.concurrent.Callable;

public class GrowthAndFill implements Callable<UserInfo> {
    UserInfo userInfo;

    public GrowthAndFill(UserInfo userInfo) {
        this.userInfo = userInfo;
    }

    @Override
    public UserInfo call() throws Exception {
        userInfo.setAge(30);
        Thread.sleep(180);
        return userInfo;
    }
}
代码语言:javascript
复制
package com.example.core.mydemo.bean;

import com.example.core.mydemo.bean.callable.BonusAndFill;
import com.example.core.mydemo.bean.callable.GrowthAndFill;
import com.example.core.mydemo.bean.callable.UserAndFill;
import com.example.core.mydemo.json2.GsonUtils;

import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.Future;

public class CallableTest {
    public static void main(String[] args) throws Exception{
        long now = System.currentTimeMillis();

        UserInfo userInfo = new UserInfo();
        ExecutorService executorService = Executors.newFixedThreadPool(10);

        /**
         * userInfo={"username":"刘德华","sex":"男","age":30},time=539
         * 类似串行了
         */
       /* UserAndFill userAndFill = new UserAndFill(userInfo);
        Future<UserInfo> future1 = executorService.submit(userAndFill);
        userInfo = future1.get();

        BonusAndFill bonusAndFill = new BonusAndFill(userInfo);
        Future<UserInfo> future2 = executorService.submit(bonusAndFill);
        userInfo = future2.get();

        GrowthAndFill growthAndFil = new GrowthAndFill(userInfo);
        Future<UserInfo> future3 = executorService.submit(growthAndFil);
        userInfo = future3.get();*/


        /**
         * userInfo={"username":"刘德华","sex":"男","age":30},time=208
         */
        UserAndFill userAndFill = new UserAndFill(userInfo);
        BonusAndFill bonusAndFill = new BonusAndFill(userInfo);
        GrowthAndFill growthAndFil = new GrowthAndFill(userInfo);

        Future<UserInfo> future1 = executorService.submit(userAndFill);
        Future<UserInfo> future2 = executorService.submit(bonusAndFill);
        Future<UserInfo> future3 = executorService.submit(growthAndFil);


        userInfo = future1.get();
        userInfo = future2.get();
        userInfo = future3.get();

        executorService.shutdownNow();

        long end = System.currentTimeMillis();
        System.out.println("userInfo=" + GsonUtils.toJson(userInfo) + ",time=" + (end-now));
    }
}
本文参与 腾讯云自媒体同步曝光计划,分享自作者个人站点/博客。
原始发表:2024-09-27,如有侵权请联系 cloudcommunity@tencent.com 删除

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

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

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

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
相关产品与服务
GPU 云服务器
GPU 云服务器(Cloud GPU Service,GPU)是提供 GPU 算力的弹性计算服务,具有超强的并行计算能力,作为 IaaS 层的尖兵利器,服务于生成式AI,自动驾驶,深度学习训练、科学计算、图形图像处理、视频编解码等场景。腾讯云随时提供触手可得的算力,有效缓解您的计算压力,提升业务效率与竞争力。
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档