ExecuteRun「建议收藏」

ExecuteRun「建议收藏」ExecuteRun

大家好,又见面了,我是你们的朋友全栈君。

package main.com.lift;

import com.google.common.collect.Lists;
import com.google.common.collect.Maps;

import java.lang.annotation.Target;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;
import java.util.Queue;
import java.util.concurrent.*;

public class ExecuteRun {
    public double runanswer(List<List<Integer>> produceElem, LinkedBlockingQueue<Integer> queue)throws  Exception{
        CyclicBarrier cyclicBarrier=new CyclicBarrier(produceElem.size());
        List<Thread> threads= Lists.newArrayList();
        Map<String, FutureTask> futureTaskMap= Maps.newHashMap();
        for(int i=0;i<produceElem.size();i++){
            Task task=new Task(cyclicBarrier,produceElem.get(i),queue);
            FutureTask futureTask=new FutureTask(task);
            Thread childThread=new Thread(futureTask,"lift"+i);
            threads.add(childThread);
            childThread.start();
            futureTaskMap.put("lift"+i,futureTask);
        }
        for (Thread thread:threads){
            try{
                thread.join();
            }catch (Exception e){
                e.printStackTrace();
            }
        }
        //数据分布好后,每个电梯消费各自队列,每次消费5个人
        Map<String,FutureTask> futureTaskMap2=Maps.newHashMap();
        for(int i=0;i<produceElem.size();i++){
            //获取该电梯的等待队列
            Queue<Integer> waitqueue=(Queue<Integer>) futureTaskMap.get("lift"+i).get();
            Task2 task2=new Task2(waitqueue);
            FutureTask<Double> futureTask=new FutureTask<>(task2);
            Thread childThread=new Thread(futureTask,"lift"+i);
            futureTaskMap2.put("lift"+i,futureTask);
            childThread.start();
        }
        //存放所有电梯的运行时间
        Map<String,Double> sumTime=Maps.newHashMap();

        //**********************************************不开启监控***************************************************
//        for(Map.Entry<String,FutureTask> entry:futureTaskMap2.entrySet()){
//            String item2=entry.getKey();
//            FutureTask intlift=entry.getValue();
//            //如果自己的线程消费完了,区分担其他线程的
//            if(intlift.get()!=null){
//                try{
//                    sumTime.put(item2,(double)intlift.get());
//
//                }catch (Exception e){
//                    e.printStackTrace();
//                }
//            }
//        }
        //****************************************开启监控*******************************************
        //监控线程是否结束
        int mapsize=0;
        List<Thread> allThread=Lists.newArrayList();
        List<Callable> allCallable=Lists.newArrayList();
        do{
            for(Map.Entry<String,FutureTask> entry:futureTaskMap2.entrySet()){
                String item2=entry.getKey();
                FutureTask intlift=entry.getValue();
                //如果自己的线程消费完了后,去分担其他线程的
                if(intlift.get()!=null){
                    mapsize++;
                    try{
                        sumTime.put(item2,(double)intlift.get());
                    }catch (Exception e){
                        e.printStackTrace();
                    }
                    Thread t=new Thread(new Runnable() {
                        @Override
                        public void run() {
                            //一个电梯运完自己队列里的人后,去其他的队列中去找有没有符合自己能到达楼层的人
                            List<Integer> lift=produceElem.get(Integer.parseInt(item2.substring(4,item2.length())));
                            //存放新建的线程
                            Map<String,FutureTask> futureTaskMapson=Maps.newHashMap();
                            futureTaskMap.forEach((item,intperson) ->{
                                try{
                                    Queue<Integer> waitqueue2=(Queue<Integer>) intperson.get();
                                    //检查遍历到的队列,如果有元素在可以消费的队列中,就加入消费,并删除
                                    synchronized (waitqueue2){
                                        if(!waitqueue2.isEmpty()){
                                            Queue<Integer> waitqueuSon=Lists.newLinkedList();
                                            for(int x=0;x<waitqueue2.size();x++){
                                                if(lift.contains(x)){
                                                    waitqueuSon.offer(x);
                                                    ((LinkedList<Integer>) waitqueue2).remove(x);
                                                    x--;
                                                }
                                                if(waitqueuSon.size()==5 || waitqueue2.size()==0){
                                                    break;
                                                }
                                            }
                                            if(waitqueuSon.size()<=0){
                                                return;
                                            }
                                            //取到后新启一个线程消费
                                            Task2 taskson=new Task2(waitqueuSon);
                                            FutureTask<Double> futureTask=new FutureTask<>(taskson);
                                            if(futureTaskMapson.get(item2) ==null){
                                                futureTaskMapson.put(item2,futureTask);
                                            }else {
                                                //否则就先拿到之前运行的时间,在放进去新的执行
                                                double itemson=(double)futureTaskMapson.get(item2).get();
                                                sumTime.put(item2,sumTime.get(item2)+itemson);
                                                futureTaskMapson.put(item2,futureTask);
                                            }
                                            allCallable.add(taskson);
                                            futureTask.run();
                                        }
                                    }
                                } catch (Exception e) {
                                    e.printStackTrace();
                                }
                            });
                            int mapsize2=0;
                            //所有运行完了之后就再次清算新建的自消费线程
                            do{
                                for(Map.Entry<String,FutureTask> entry2:futureTaskMapson.entrySet()){
                                    String item2son=entry2.getKey();
                                    FutureTask intliftson=entry2.getValue();
                                    try{
                                        if(intliftson.get()!=null){
                                            mapsize2++;
                                            try{
                                                double itemson=(double)intliftson.get();
                                                if(sumTime.get(item2son)!=null){
                                                    sumTime.put(item2son,sumTime.get(item2son)+itemson);
                                                }
                                            }catch (Exception e){
                                                e.printStackTrace();
                                            }
                                        }
                                    }catch (Exception e){
                                        e.printStackTrace();
                                    }
                                }
                            }while (mapsize2<futureTaskMapson.size());
                        }
                    });
                    allThread.add(t);
                    t.start();
                }
            }
        }while (mapsize<futureTaskMap2.size());
        //都结束了,看结果
        System.out.println(" ");
        sumTime.forEach((k,v) ->{
            System.out.println(k+" : "+ v);
        });
        allThread.forEach(thread -> {
            try{
                thread.join();
            }catch (Exception e){
                e.printStackTrace();
            }
        });
        double maxtime=0;
        for(Map.Entry<String,Double> entry:sumTime.entrySet()){
            if(maxtime<entry.getValue()){
                maxtime=entry.getValue();
            }
        }
        return  maxtime;
    }

}

 

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请联系我们举报,一经查实,本站将立刻删除。

发布者:全栈程序员-站长,转载请注明出处:https://javaforall.net/105945.html原文链接:https://javaforall.net

(0)
全栈程序员-站长的头像全栈程序员-站长


相关推荐

  • request中的方法_requests发送get请求

    request中的方法_requests发送get请求request.getRealPath不推荐使用request.getRealPath(“”)这个方法已经不推荐使用了,那代替它的是什么方法Deprecated.AsofVersion2.1oftheJavaServletAPI,useServletContext.getRealPath(java.lang.String)instead.request.getSess

    2022年9月15日
    1
  • Mesos 配置项解析

    Mesos 配置项解析

    2022年2月4日
    35
  • java快捷键

    java快捷键快捷键:如果想修改快捷键(setting->keymap)。Ctrl+Space完成类、方法、变量名称的自动输入,这个快捷键是我最经常使用的快捷键了,它可以完成类、方法、变量名称的自动录入,很方便Ctrl+N(Ctrl+Shift+N)跳转到指定的java文件(其它文件)这个功能很方便,至少我不用每回都在一长串的文件列表里找寻我想要编辑的类文件和jsp文件了Ctrl+B跳转到定义处这个就不用多说了,好象是个IDE就会提供的功能Ctrl+Alt+T用*来围绕

    2022年7月7日
    22
  • dubbo负载均衡策略解析

    dubbo负载均衡策略解析dubbo负载均衡策略前言:在上一篇博客中,介绍了zookeeper作为dubbo的注册中心是如何工作的,有一个很重要的点,我们的程序是分布式应用,服务部署在几个节点(服务器)上,当消费者调用服务时,zk返回给dubbo的是一个节点列表,但是dubbo只会选择一台服务器,那么它究竟会选择哪一台呢?这就是dubbo的负载均衡策略了,本篇博客就来聚焦dubbo的负载均衡策略。本篇博客的目录一:负载均衡介绍1.1:负载均衡简介以下是wikipedia对负载均衡的定义:负载均衡改善…

    2022年7月11日
    19
  • sql2012安装错误代码0x84b10001_sql2008配置系统未能初始化

    sql2012安装错误代码0x84b10001_sql2008配置系统未能初始化【错误描述】Sql2008安装启动失败,弹出 配置系统未能初始化0x84B10001【解决方法】先安装sql2008sp3补丁.然后再安装Sql2008,最后再装一次sp3补丁.

    2022年9月11日
    3
  • S3服务器规格和性能指标,云服务器s3与s4

    S3服务器规格和性能指标,云服务器s3与s4云服务器s3与s4内容精选换一换创建一台或多台云服务器。V1.1版本创建云服务器的接口兼容了V1版本创建云服务器(按需)的功能,同时合入新功能,支持创建包年/包月的弹性云服务器。本接口为异步接口,当前创建云服务器请求下发成功后会返回job_id,此时创建云服务器并没有立即完成,需要通过调用查询任务的执行状态查询job状态,当Job状态为SUCCESS时代表云服务器创在申请SAPS/4HAN…

    2022年5月11日
    59

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注

关注全栈程序员社区公众号