多线程分派任务的MW组件初步设想?持续更新...

public  class Master<E> {

    //存放E类型任务的容器,只对taskQuueu进行写
    private volatile BlockingQueue<? super E> TaskQueue;
    //存放每一个worker任务结果的集合,只对result进行读
    private volatile BlockingQueue<? super Object> resultQueue;

    //存放worker的合集
    private volatile Map<String,Callable> wokersMap;
    //执行任务的线程池
    private  ThreadPoolExecutor threadPool;
    //存放运行结果
    //该Master运行完成次数
    //TODO
    private AtomicInteger finishNum = new AtomicInteger(-1);
    //初始化capacity
    private static Integer DEFAULT_CAPACITY = 1200;
    //使用锁
    private volatile Lock lock = new ReentrantLock();
    //任务的future队列
    private List<Future> futureList = new ArrayList<>();
    //整个master标志
    private AtomicBoolean flag = new AtomicBoolean(false);
    //日志
    private final static Logger logger = LoggerFactory.getLogger(Master.class);


    /**
     * 构造一个大小为30,任务容量为capacity的线程池,初始化workQueue和workerMap
     * @param worker
     * @param workThreads
     * @param capacity
     */
    public Master(Worker worker, Integer workThreads, Integer capacity){
        threadPool = new ThreadPoolExecutor(
                10,
                30,
                1000L,
                TimeUnit.SECONDS,
                new ArrayBlockingQueue<Runnable>(capacity),
                new MWRejection());

        TaskQueue = new ArrayBlockingQueue<E>(capacity);
        wokersMap = new HashMap();
        resultQueue = new LinkedBlockingQueue<Object> ();

        worker.setTaskQueue(this.TaskQueue);
        worker.setResultQueue(this.resultQueue);

        for (Integer i = 0; i < workThreads; i++) {
            wokersMap.put(Integer.toString(i),worker);
        }
        logger.info("{}个worker已经启动",workThreads);

    }

    //构造
    public Master(Worker worker, Integer workThreads){
        this(worker,workThreads,DEFAULT_CAPACITY);
    }

    //执行任务
    public void executeThread(){
        wokersMap.forEach((k,v)->{
            Future future = threadPool.submit(v);
            futureList.add(future);
        });
    }

    //提交任务
    public void submitTask(E task){
        lock.lock();
        try {
            this.TaskQueue.put(task);
            logger.info("task add {}",task.toString());
        }catch (Exception e){
            System.out.println("提交失败");
            logger.error("{}提交失败",task);
        }finally {
            lock.unlock();
        }
    }

    //过程中间判断,非阻塞
    public boolean isComplete(){
        lock.lock();
        try{
            if(futureList != null)
                for (Future f : futureList) {
                    if (!f.isDone()) {
                        //任务尚未做完
                        return false;
                    }
                }
            return true;
        }catch (Exception e){
            return flag.get();
        }finally {
            System.out.println("准备释放lock");
            lock.unlock();
        }
    }

    //最终状态判断,阻塞
    public  Boolean  get(){
        lock.lock();
        try {
            flag.set(true);
            for (Future f : futureList) {
                if(!(Boolean) f.get()){
                    flag.set(false);
                    System.out.println("future failure");
                }
            }
        } catch (Exception e)  {
            flag.set(false);
            e.printStackTrace();
        }finally {
            lock.unlock();
        }
        threadPool.shutdownNow();
        System.out.println("完成了"+flag.get());
        return flag.get();
    }


    //计算结果方法

    /**
     * 将Worker类型传入
     * @param
     * @return
     */
    public Integer getFinishedSum(){
        lock.lock();
         Integer sum = 0;
        try{
            if (this.get()){
                if( resultQueue != null/* && threadPool.isTerminated()*/){
                    this.finishNum.incrementAndGet();
                    for (Object v:
                         resultQueue) {
                        if(v != null){
//                            this.finishNum.incrementAndGet();
                            sum += (Integer) v;
                        }
                    }
                }
            }
        }catch (Exception e){
            e.printStackTrace();
        }finally {
            lock.unlock();
        }
        return sum;

    }

}


@ConcurrentSafe
public abstract class Worker<E> implements Callable {

    //只对taskQueue进行读
    private  BlockingQueue<? extends E> taskQueue;
    //只对resultQueue进行写
    private  BlockingQueue<? super Object> resultQueue;

    //使用锁
    private volatile Lock lock = new ReentrantLock();
    //统计校验
    private static AtomicLong TaskID = new AtomicLong(0L);


    public void setResultQueue(BlockingQueue<Object> resultQueue) {
        this.resultQueue = resultQueue;
    }

    public void setTaskQueue(BlockingQueue<E> taskQueue) {
        this.taskQueue = taskQueue;
    }


    private  E getOne(){
        lock.lock();
            try {
                if(taskQueue.size() != 0 && !taskQueue.isEmpty())
                    return this.taskQueue.poll();
                return null;
            } catch (Exception e) {
                e.printStackTrace();
                return null;
            }finally {
                lock.unlock();
            }
    }

    public Boolean call() {
        while(true){
            try {
                E task = getOne();
                if(task == null){
                    break;
                }
                Object result = handle(task);
                resultQueue.put(result);
                TaskID.incrementAndGet();
            }catch (Exception e){
                e.printStackTrace();
                return false;
            }
        }
        return true;
    }

    //处理任务的方法
    public abstract Object handle(E task);

    //获取任务的类型
    public abstract  Class<E> getTaskClass();

    public static long getTaskID(){
        return TaskID.get();
    }

}

猜你喜欢

转载自blog.csdn.net/weixin_36708538/article/details/81562636