啥?

  博客园  :: 首页  :: 新随笔  :: 联系 :: 订阅 订阅  :: 管理
import com.alibaba.fastjson.JSONObject;

import java.text.SimpleDateFormat;
import java.util.*;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.locks.ReentrantLock;
import java.util.function.Supplier;

/**
 * 实体类中添加多个执行漫的值,使用多线程的方式执行
 */
public class AsynInitEntitiesUtil<T> {

    private ReentrantLock lock;
    private T entities;
    private Integer timeOut; //执行超时时间
    private TimeUnit unit; //执行超时时间单位
    private ExecutorService executorService;
    private List<Supplier<T>> supplierList;

    public AsynInitEntitiesUtil(T entities, int timeOut, TimeUnit unit) {
        this.supplierList = new ArrayList<>();
        this.lock = new ReentrantLock();
        this.entities = entities;
        this.timeOut = timeOut;
        this.unit = unit;
        this.executorService = Executors.newCachedThreadPool();
    }


    public static void main(String[] args) {
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        System.out.println(simpleDateFormat.format(new Date()));
        UsersInfo usersInfo = new UsersInfo();
        usersInfo = (UsersInfo) new AsynInitEntitiesUtil(usersInfo, 10, TimeUnit.SECONDS)
                .addTask(() -> new UsersInfo().setId("20"))
                .addTask(() -> new UsersInfo().setName("李明"))
                .addTask(() -> new UsersInfo().setAddress("河北"))
                .execute();


        System.out.println(JSONObject.toJSONString(usersInfo));
        System.out.println(simpleDateFormat.format(new Date()));
    }

    /**
     * 执行多任务
     *
     * @return
     */
    public T execute() {
        try {
            CountDownLatch cd = new CountDownLatch(supplierList.size());
            for (Supplier<T> supplier : supplierList) {
                threadRun(() -> {
                    try {
                        initMap(supplier.get());
                    } catch (Exception e) {
                        e.printStackTrace();
                    } finally {
                        cd.countDown();
                    }
                });
            }
            cd.await(this.timeOut, unit);
        } catch (InterruptedException e) {
            e.printStackTrace();
        } finally {
            try {
                if (this.executorService != null) {
                    this.executorService.shutdown();
                }
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
        return this.entities;
    }

    public AsynInitEntitiesUtil addTask(Supplier<T> supplier) {
        supplierList.add(supplier);
        return this;
    }

    public void initMap(T t) {
        lock.lock();
        try {
            String oldParams = JSONObject.toJSONString(entities);
            JSONObject oldOb = JSONObject.parseObject(oldParams);

            String newParams = JSONObject.toJSONString(t);
            JSONObject newOb = JSONObject.parseObject(newParams);
            oldOb.putAll(newOb);
            this.entities = (T) JSONObject.parseObject(oldOb.toJSONString(), t.getClass());
        } finally {
            lock.unlock();
        }
    }

    /**
     * 线程异步执行
     *
     * @param
     */
    public void threadRun(VoidTask voidTask) {
        executorService.execute(() -> {
            voidTask.execute();
        });
    }

    @FunctionalInterface
    private interface VoidTask {
        void execute();
    }
}

 

posted on 2024-05-08 16:59  啥?  阅读(4)  评论(0编辑  收藏  举报