微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

为什么while循环出来的经过时间值小于错误时间总和?

如何解决为什么while循环出来的经过时间值小于错误时间总和?

MyThread1 m1 = new MyThread1();
m1.start();
......................................................
int k1 = 0;   
long start1 = System.currentTimeMillis();
boolean b1 = true;
long errorTime = 0;
while (b1){
    k1=m1.k;       
    if (k1==1000000){
        long end1 = System.currentTimeMillis();
        System.out.println("thread 1-1 time = " + (end1-start1));
        break;
    }
    else if (System.currentTimeMillis()-start1>10){
        errorTime += System.currentTimeMillis()-start1;
        System.out.println("thread1 needs so much time,we had to stop waiting for him");           
    }
}
long end1 = System.currentTimeMillis();
System.out.println("thread 1 time = " + (end1-start1));
System.out.println("thread 1 error time = " +errorTime);
........................................................

class MyThread1 extends Thread{
    int k=0;
    private int p=0;
    public void run(){
        for (int i=0; i<=1000000;i++){                
            p=i;
            if (p==1000000){
                k=p;                   
            }
        }
    }
}


Output:
thread1 needs so much time,we had to stop waiting for him
thread1 needs so much time,we had to stop waiting for him
thread 1-1 time = 14
thread 1 time = 32
thread 1 error time = 286

为什么“线程 1 次”小于“线程 1 错误时间”?

我认为一定是 >“线程 1 错误时间”。

为什么我认为它必须是 > 而不是

因为我总结了错误时间值以获取有关“失败尝试需要多少时间?”的信息。并且根据逻辑,整个经过的时间必须大于而不是小于错误时间值总和。

我不知道问题的根源是什么。

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