消失的小海蒂 2021-03-02 17:49 采纳率: 0%
浏览 168
已结题

在java的main函数里弄了一个无限循环程序,过了几天CPU满载

List<Device> list = new ArrayList<>();
        for (int i = 1; i < 123; i++) {
            list.add(new Device(i));
        }
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");

        while (true) {
            String date = format.format(new Date());
            for (Device d : list) {
                d.setData();
                String message = new DeviceData(d.getIot(), d.getAmp(), d.getVolt(), date).toString();
                producer.pub("/iot="+d.getIot(),message,2);
            }

            Thread.sleep(2000);
        }

这个里面的内容就是模拟一百来台设备,每台设备的对象调用一个生成随机数据的方法并set,每隔几秒通过MQTT把message发布出去,刚开始跑的时候比较正常,过了几天以后在linux服务器下我用top查看发现CPU%达到好几百 得重启这个应用才行 有没有办法改善这种情况?

 

public class Device {
    private Integer iot;
    private Float amp;
    private Float volt;
    private Boolean isLifeCycle = false;
    private Integer lifeCycle = 60;
    private Integer interval = 10;
    private Integer time=0;

    public Device(Integer iot) {
        super();
        this.iot = iot;
    }

    public void setData() {
        Random r = new Random();
        this.setTime(this.getTime() + 1);
        if (!isLifeCycle) {
            if (this.time > this.interval) {
                if (r.nextFloat() > 0.8F) {
                    this.setTime(0);
                    this.setInterval(r.nextInt(20) + 10);
                    this.setIsLifeCycle(true);
                }
            }
            this.setAmp(r.nextFloat());
            this.setVolt(r.nextFloat());
        } else {
            if (this.time > this.lifeCycle) {
                this.setTime(0);
                this.setLifeCycle(r.nextInt(10) + 55);
                this.setIsLifeCycle(false);
            }
            this.setAmp(598f+r.nextFloat()*194f);
            this.setVolt(25.9f+r.nextFloat()*6.2f);
        }
    }
}
  • 写回答

3条回答 默认 最新

  • 歇歇 2021-03-02 20:47
    关注
    List<Device> list = new ArrayList<>();
    
    
            for (int i = 1; i < 123; i++) {
    
    
                list.add(new Device(i));
    
    
            }
    
    
            SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
    
    
     
    
    
            while (true) {
    
    
                String date = format.format(new Date());
    
    
                for (Device d : list) {
    
    
                    d.setData();
    
    
                    String message = new DeviceData(d.getIot(), d.getAmp(), d.getVolt(), date).toString();
    
    
                    producer.pub("/iot="+d.getIot(),message,2);
                    String =null;
    
    
                }
    
    
                date =null;
    
    
                Thread.sleep(2000);
    
    
            }
    评论

报告相同问题?

悬赏问题

  • ¥15 请问下这个红框里面是什么文档或者记事本编辑器
  • ¥15 机器学习教材中的例题询问
  • ¥15 求.net core 几款免费的pdf编辑器
  • ¥15 为什么安装HCL 和virtualbox之后没有找到VirtualBoxHost-OnlyNetWork?
  • ¥15 C# P/Invoke的效率问题
  • ¥20 thinkphp适配人大金仓问题
  • ¥20 Oracle替换.dbf文件后无法连接,如何解决?(相关搜索:数据库|死循环)
  • ¥15 数据库数据成问号了,前台查询正常,数据库查询是?号
  • ¥15 算法使用了tf-idf,用手肘图确定k值确定不了,第四轮廓系数又太小才有0.006088746097507285,如何解决?(相关搜索:数据处理)
  • ¥15 彩灯控制电路,会的加我QQ1482956179