当前位置: 首页 > 工具软件 > Fork CMS > 使用案例 >

ForkJoin

陈斌
2023-12-01
package cn.fulong.cms.web.column.utils;

import cn.fulong.cms.web.column.service.ColumnService;
import cn.fulong.common.config.Platform;
import cn.fulong.common.service.impl.BaseServiceImpl;
import org.springframework.stereotype.Component;
import java.util.Arrays;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.concurrent.CopyOnWriteArrayList;
import java.util.concurrent.RecursiveTask;
import java.util.stream.Collectors;

/**
 * ForkJoin 多线程  提升~50%
 * @ProjectName: utils
 * @Package: cn.fulong.web.forkjoin
 * @Description: java类作用描述
 * @Author: yu_du_chen
 * @CreateDate: 2019/9/10 16:26
 * @Version: 1.0
 */
@Component
public class CreateTable {
    
    public static class MyTask extends RecursiveTask<CopyOnWriteArrayList> {
		//maps为需要进行处理的数据集合,使用CopyOnWriteArrayList保证数据安全
        private CopyOnWriteArrayList<Map<String, Object>> maps;
        public MyTask(List<Map<String, Object>> maps) {
            this.maps = new CopyOnWriteArrayList(maps);
        }

        /* (non-Javadoc)
         * @see java.util.concurrent.RecursiveTask#compute()
         */
        @Override
        protected CopyOnWriteArrayList compute() {
            int sourceLen = maps.size();
            // 如果条件成立,说明任务中要进行处理的集合还不够小
            if(sourceLen > 1) {
                int midIndex = sourceLen / 2;
                // 拆分成两个子任务
                MyTask task1 = new MyTask(maps.subList(0, midIndex));
                task1.fork();
                MyTask task2 = new MyTask(maps.subList(midIndex, sourceLen));
                task2.fork();
                // 将两个有序的数组,合并成一个有序的数组
                CopyOnWriteArrayList list1 = task1.join();
                CopyOnWriteArrayList list2 = task2.join();
                list1.addAll(list2);
                return list1;
            }
            // 否则说明集合中只有一个或者两个元素,可以进行这两个元素的数据库插入了
            else {
                if(sourceLen > 0) {
                	//这里是项目框架方法,目的是获取接口调用方法
                    ColumnService columnService = (ColumnService) Platform.getInstance().getBean("columnServiceImpl");
                    //maps.get(0) (*注sourceLen<=1) 说明集合中只有一个元素了
                    columnService.setCmsTableIng(maps.get(0));
                }
                return maps;
            }
        }

    }

}

	@Override
    public boolean setCmsTableIng(Map<String, Object> map) {
    	//进行创建表处理
        CmsChannel cmsChannel = (CmsChannel) baseTransaction.get(CmsChannel.class,map.get("ID").toString());
        String sql ;
        if ("footer".equals(cmsChannel.getTemplateId())||"header".equals(cmsChannel.getTemplateId())){
            sql = "CREATE TABLE CMS_"+map.get("UUID")+" SELECT  *  FROM  CMS_" + map.get("ID") ;
        }else{
            sql = "CREATE TABLE CMS_"+map.get("UUID")+" SELECT  *  FROM  CMS_" + map.get("ID") + "  WHERE 1 = 2 ";
        }
        jdbcTemplate.update(sql);
        String sql2 = "UPDATE CMS_"+map.get("UUID")+" set CHANNEL_ID = ?";
        jdbcTemplate.update(sql2,map.get("UUID"));
        return true;
    }
 类似资料: