Task2 计算电梯运行时间线程

Task2 计算电梯运行时间线程Task2 计算电梯运行时间线程

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

package main.com.lift;

import com.google.common.collect.Lists;
import com.sun.corba.se.spi.presentation.rmi.IDLNameTranslator;

import java.math.BigDecimal;
import java.util.List;
import java.util.Queue;
import java.util.TreeSet;
import java.util.concurrent.Callable;

public class Task2 implements Callable<Double> {
    public static final double  v=2.5;//电梯平稳运行后的速度
    public static final double a=1; //电梯加速度
    public static final double t1=2.5/1;//电梯加速到平稳速度后
    public static final double h1=2.8;//每层楼高度
    public static final double rt=5;//电梯每到达一层的停留时间

    Queue<Integer> waitqueue;
    public Task2(Queue<Integer> waitqueue){
        this.waitqueue=waitqueue;
    }

    @Override
    public Double call() throws Exception {
        //存放运完该队列所有人需要的总时间、
        double sumTime=0;
        //一次性从等待队列中取电梯的最大容量的个数
        while(!waitqueue.isEmpty()){
            int maxvolume =10;
            List<Integer> oncerun= Lists.newArrayList();
            do{
                synchronized (waitqueue){
                    if(!waitqueue.isEmpty()){
                        oncerun.add(waitqueue.poll());
                    }else {
                        break;
                    }
                }
                maxvolume--;
            }while (maxvolume>0);
            //电梯装满人后开始运行
            //1,先排序,并去重
            List<Integer> lists=Lists.newArrayList(new TreeSet<>(oncerun));
            //2,开始运行,每到达一层让其睡眠到达改成需要的时间,还有睡眠停留在改层需要的时间
            double timesum=0;
            int tmp=0;
            for(int k=0;k<lists.size();k++){
                //到达该层后先睡眠5秒
                Thread.sleep((long)rt);
                timesum+=rt;
                double time=getnTime(lists.get(k)-tmp);
                timesum+=time;
                //睡眠上楼需要的时间
                Thread.sleep((long)time);
                tmp=lists.get(k);
            }
            //下电梯需要的时间,并睡眠,直接直达
            double downTime=getnTime(getMaxFromList(lists));
            Thread.sleep((long)downTime);
            sumTime+=(timesum+downTime);
        }
        return sumTime;
    }

    /**
     * 获取lists中最大值
     * @param list
     * @return
     */
    public Integer getMaxFromList(List<Integer> list){
        int temp=0;
        for(Integer ints:list){
            if(ints>temp){
                temp=ints;
            }
        }
        return temp;
    }

    /**
     * 运行n层需要的时间
     * @param n
     * @return
     */
    public double getnTime(int n){
        //电梯只在加减速中运行需要的时间
        if(!isgetspeedest(h1*n)){
            double t1high=Math.sqrt(chufa(h1*n,a));
            return t1high*2;
        }else {
            double t2high=chufa(h1*n-getaddormineS(),v);
            return t2high+t1*2;
        }
    }
    public boolean isgetspeedest(double h){
        //刚好达到最大速度时,加速和减速走的总路程
        double s=getaddormineS();
        return s<h;
    }

    public double getaddormineS(){//获取在加速和减速期间运行的总路程
        return 0.5*a*t1*t1*2;
    }
    public double chufa(double a,double b){
        BigDecimal beichushu=new BigDecimal(a);
        BigDecimal chushu=new BigDecimal(b);
        return beichushu.divide(chushu).doubleValue();
    }
}

 

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

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

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


相关推荐

  • Linux tomcat安装详解

    Linux tomcat安装详解欢迎访问我的个人博客网站:http://www.yanmin99.com/一、tomcat安装1、下载JDK和Tomcat//通过wget下载wgethttp://mirrors.tuna.tsinghua.edu.cn/apache/tomcat/tomcat-8/v8.5.4/bin/apache-tomcat-8.5.4.tar.gzwgethttp://download.ora

    2022年6月2日
    32
  • Mac上一定要安装的——MacPorts「建议收藏」

    Mac上一定要安装的——MacPorts「建议收藏」 如果你习惯了使用linux里边的apt-get,那么你一定要在你的mac里安装MacPorts。以下是转载过来的关于这个软件的简介和安装步骤:MacPorts简介MacPorts,曾经叫做DarwinPorts,是一个软件包管理系统,用来简化MacOSX和Darwin操作系统上软件的安装。它是一个用来简化自由软件/开放源代码软件的安装的自由/开放源代码项目,与Fin…

    2022年9月21日
    3
  • 函数模版特化(类模板的实例化在什么阶段)

    本文转自:https://www.cnblogs.com/dracohan/p/3401660.html 转来收藏以便查阅,感谢原作者今天在写代码时,遇到了模板和特化,在网上找了资料后问题呗一一解决,转载此文用于以后查阅,感谢原创者。其中增加了我自己的总结:特化函数与模板函数的区别:(1)、模板函数的T参数只能传入类类型的参数;特化函数的参数只能传入对应的参数类型,基本类型或类类型。…

    2022年4月17日
    64
  • python2+selenium爬取笔趣读小说

    python2+selenium爬取笔趣读小说python2+selenium爬取笔趣读小说 #!/usr/bin/envpython#coding=utf-8fromseleniumimportwebdriverimporttimefrombs4importBeaut…

    2025年8月19日
    3
  • jupyter的代码能用pycharm运行吗_win10安装jupyter

    jupyter的代码能用pycharm运行吗_win10安装jupyter在Pycharm中安装及使用Jupyter(图文详解)文章目录在Pycharm中安装及使用Jupyter(图文详解)一、材料二、安装Jupyter三、配置Jupyter四、使用Jupyter1.使用Cell2.使用jupyterMarkdownPycharm更新了对Jupyter的功能支持,结合IntelliJ的自动补全代码,自动格式化代码,执行调试…

    2022年8月26日
    6
  • Google Earth Engine学习笔记——介绍和入门

    Google Earth Engine学习笔记——介绍和入门   最近接触GoogleEarthEngine,觉得很好玩,也很有应用前景,关键是免费的地理计算云平台。所以想认真学习下,学习过程中作些小的总结和记录,资料来源均为网络或GoogleEarthEngineAPI指南,今天先讲讲入门的知识。1、GoogleEarthEngine介绍   GoogleEarthEngine是Google提供的对大量全球尺度地球科学资料(尤其是…

    2022年9月19日
    3

发表回复

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

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