gpt4 book ai didi

java - 不同实例的多线程仍然产生相同的结果,如何克服这个问题?

转载 作者:太空宇宙 更新时间:2023-11-04 09:18:49 26 4
gpt4 key购买 nike

     public class MultiThreadingRandom {
public static void main(String[] args) throws InterruptedException {
MultiThreadingRandom multiThreadingRandom = new MultiThreadingRandom();

ExecutorService executorService = Executors.newFixedThreadPool(2);

geneRan r1= new geneRan();

geneRan r2= new geneRan();

executorService.submit(r1);


executorService.submit(r2);

executorService.shutdown();



}
}
class geneRan implements Runnable{

int rand_int1=0;

@Override
public void run() {
// TODO Implement this method
Random rand = new Random();
rand_int1 = rand.nextInt(1000);
System.out.println(rand_int1);


// System.out.println(ai.getAndIncrement());
}
}

这个程序有时会给出 2 个不同的输出,但有时会给出相同的输出。

实际上,我对这两个线程使用了 2 个不同的对象,所以为什么在某些情况下会给出相同的结果。

无论如何,我只传递 2 个不同的对象及其线程安全代码。那么我如何才能确保在任何情况下生成 2 个不同的随机数。

最佳答案

正如已经评论过的,这与多线程无关。由于数字是随机的,因此它们也有可能相同。

如果你想要 2 个不相同的随机数,你可以这样做:

//Create List of integers
List<Integer> numberPool = new ArrayList<>();
//Initialize list with numbers from 1 to 1000
for(int num = 1; num <= 1000 ; num++) {
numberPool.add(num);
}
//Randomly shuffle list
Collections.shuffle(numberPool);
//Get first number
System.out.println(numberPool.get(0));
//Get second number
System.out.println(numberPool.get(1));

如果你想以多线程方式访问 numberPool,你可以这样做:

public class NumGeneratorThreadSafe{

List<Integer> numberPool = new ArrayList<>();
private int counter = 0;

public NumGeneratorThreadSafe() {
for(int i = 1; i <= 1000 ; i++) {
this.numberPool.add(i);
}
Collections.shuffle(this.numberPool);
}

public synchronized Integer getRandomNumber() {
return this.numberPool.get(this.counter++);

}

public synchronized void resetCounter() {
this.counter = 0;
}

}

希望这有帮助。

编辑:为了在线程中使用该类:


public class MultiThreadingRandom {

public static void main(String[] args) throws InterruptedException {

ExecutorService executorService = Executors.newFixedThreadPool(2);

NumGeneratorThreadSafe numGenerator = new NumGeneratorThreadSafe();

GeneRan r1 = new GeneRan(numGenerator);
GeneRan r2 = new GeneRan(numGenerator);

executorService.submit(r1);
executorService.submit(r2);

executorService.shutdown();

}
}


class GeneRan implements Runnable{

private NumGeneratorThreadSafe numGenerator;

public GeneRan(NumGeneratorThreadSafe numGenerator) {
this.numGenerator = numGenerator;
}

@Override
public void run() {
int randInt = this.numGenerator.getRandomNumber();
System.out.println(randInt);
}
}

关于java - 不同实例的多线程仍然产生相同的结果,如何克服这个问题?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58602642/

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