使用Random.nextInt()来解决程序是否有可能冻结?
我的程序就像:
class CustomizedAction extends AbstractAction {
@Override
public void actionPerformed(ActionEvent e) {
clearEverything();
//Make a new Random instance
Random rnd = new Random();
//There is a loop that runs 6 times,
//for each time I will processing the value generated by rnd.nextInt()
for(loop 6 times) {
anArrayList = process(rnd.nextInt(), rnd.nextInt());
}
//Then I have another loop which runs for 12 times.
//For each time, I generated a number and do some comparing the
//new born with the previous generated numbers
//to try to make the numbers from list2 different with list1.
for(loop 12 times) {
int aNumber = rnd.nextInt();
while(anArrayList.contains(aNumber)) {
aNumber = rnd.nextInt();
}
anotherArrayList.add(aNumber);
}
}
}
有时,customAction可以工作,但有时候,customizeAction会使整个程序冻结,之前打印出来的所有以前的消息都会丢失。对我来说很奇怪,因为如果逻辑部分出现问题,控制台中的打印消息就不会丢失。
我运行循环的次数越多,customAction使程序冻结的可能性就越大。
所以,我想知道,运行nextInt()过多会导致程序冻结吗?
以下是我的程序冻结时的图像。