java8并行计算

2024-10-09 12:34:22 浏览数 (3)

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));
    }
}

0 人点赞