gpt4 book ai didi

java - 在某些情况下,计数器为负值。为什么?以及如何解决这个问题?

转载 作者:行者123 更新时间:2023-12-01 23:23:34 25 4
gpt4 key购买 nike

package threadapps;

public class Threads extends javax.realtime.RealtimeThread{

/**
* @param args the command line arguments
*/
static volatile boolean stop = false;
public static void main(String[] args) throws InterruptedException {

// TODO code application logic here
//
Thread a = new Threads();
Thread b = new Threads();
//set priority for threads
a.setPriority(4);
b.setPriority(6);
//run threads
a.start();
b.start();
//Sleep Thread
Threads.sleep(5000);
stop = true;
}

@Override
public void run(){
// for loop for counting to 10
//for(int i = 0; i<10;i++){
// System.out.println( Thread.currentThread().getName()+" The Counter Is "+i);
//}
int count = 0;
for(;!stop;)
count++;
System.out.println(Thread.currentThread().getName()+" The Count is "+count);
}
}

运行此代码并查看结果。在某些情况下它可以是负数吗?

最佳答案

如果计数达到 Integer.MAX_VALUE,那么在下一个增量时,它将是 Integer.MIN_VALUE。

关于java - 在某些情况下,计数器为负值。为什么?以及如何解决这个问题?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20329782/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com