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

  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 个不同的随机数。


回首忆惘然
浏览 95回答 2
2回答

饮歌长啸

正如已经评论过的,这与多线程无关。由于数字是随机的,因此它们也有可能相同。如果你想要 2 个不相同的随机数,你可以这样做://Create List of integersList<Integer> numberPool = new ArrayList<>();//Initialize list with numbers from 1 to 1000for(int num = 1; num <= 1000 ; num++) {&nbsp; &nbsp; numberPool.add(num);}//Randomly shuffle listCollections.shuffle(numberPool);//Get first numberSystem.out.println(numberPool.get(0));//Get second numberSystem.out.println(numberPool.get(1));如果你想以多线程方式访问 numberPool,你可以这样做:public class NumGeneratorThreadSafe{&nbsp; &nbsp; List<Integer> numberPool = new ArrayList<>();&nbsp; &nbsp; private int counter = 0;&nbsp; &nbsp; public NumGeneratorThreadSafe() {&nbsp; &nbsp; &nbsp; &nbsp; for(int i = 1; i <= 1000 ; i++) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; this.numberPool.add(i);&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; Collections.shuffle(this.numberPool);&nbsp; &nbsp; }&nbsp; &nbsp; public synchronized Integer getRandomNumber() {&nbsp; &nbsp; &nbsp; &nbsp; return this.numberPool.get(this.counter++);&nbsp; &nbsp; }&nbsp; &nbsp; public synchronized void resetCounter() {&nbsp; &nbsp; &nbsp; &nbsp; this.counter = 0;&nbsp; &nbsp; }}希望这可以帮助。编辑: 为了在线程中使用该类:public class MultiThreadingRandom {&nbsp; &nbsp; &nbsp; public static void main(String[] args) throws InterruptedException {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;ExecutorService executorService = Executors.newFixedThreadPool(2);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;NumGeneratorThreadSafe numGenerator = new NumGeneratorThreadSafe();&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;GeneRan r1 = new GeneRan(numGenerator);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;GeneRan r2 = new GeneRan(numGenerator);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;executorService.submit(r1);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;executorService.submit(r2);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;executorService.shutdown();&nbsp; &nbsp; &nbsp; }}class GeneRan&nbsp; implements Runnable{&nbsp; &nbsp; &nbsp; private NumGeneratorThreadSafe numGenerator;&nbsp; &nbsp; &nbsp; public GeneRan(NumGeneratorThreadSafe numGenerator) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;this.numGenerator = numGenerator;&nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; @Override&nbsp; &nbsp; &nbsp; public void run() {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;int randInt = this.numGenerator.getRandomNumber();&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;System.out.println(randInt);&nbsp; &nbsp; &nbsp; }}

繁花如伊

这个怎么样import java.util.Random;import java.util.concurrent.ExecutionException;import java.util.concurrent.ExecutorService;import java.util.concurrent.Executors;public class MultiThreadingRandom {&nbsp; &nbsp;public static void main(String[] args) throws InterruptedException, ExecutionException {&nbsp; &nbsp; &nbsp; ExecutorService executorService = Executors.newFixedThreadPool(2);&nbsp; &nbsp; &nbsp; Generun r1 = new Generun();&nbsp; &nbsp; &nbsp; executorService.submit(r1);&nbsp; &nbsp; &nbsp; Thread.sleep(100);&nbsp; &nbsp; &nbsp; executorService.submit(r1);&nbsp; &nbsp; &nbsp; executorService.shutdown();&nbsp; &nbsp;}}class Generun implements Runnable {&nbsp; &nbsp;int nextInt = -1;&nbsp; &nbsp;int bound = 1000;&nbsp; &nbsp;@Override&nbsp; &nbsp;public void run() {&nbsp; &nbsp; &nbsp; int temp = nextInt;&nbsp; &nbsp; &nbsp; nextInt = new Random().nextInt(bound);&nbsp; &nbsp; &nbsp; if (temp == nextInt) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;do {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; nextInt = new Random().nextInt(bound);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;} while (temp == nextInt);&nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; System.out.println(nextInt);&nbsp; &nbsp;}}
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Java