第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

不同實例的多線程仍然產(chǎn)生相同的結(jié)果,如何克服這個問題?

不同實例的多線程仍然產(chǎn)生相同的結(jié)果,如何克服這個問題?

回首憶惘然 2024-01-05 10:22:38
  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 個不同的對象,所以為什么它在某些情況下給出相同的結(jié)果。無論如何,我只傳遞 2 個不同的對象及其線程安全代碼。那么我如何才能確保在任何情況下生成 2 個不同的隨機數(shù)。
查看完整描述

2 回答

?
飲歌長嘯

TA貢獻(xiàn)1951條經(jīng)驗 獲得超3個贊

正如已經(jīng)評論過的,這與多線程無關(guān)。由于數(shù)字是隨機的,因此它們也有可能相同。


如果你想要 2 個不相同的隨機數(shù),你可以這樣做:


//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);

      }

}


查看完整回答
反對 回復(fù) 2024-01-05
?
繁花如伊

TA貢獻(xiàn)2012條經(jīng)驗 獲得超12個贊

這個怎么樣


import java.util.Random;

import java.util.concurrent.ExecutionException;

import java.util.concurrent.ExecutorService;

import java.util.concurrent.Executors;


public class MultiThreadingRandom {

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

      ExecutorService executorService = Executors.newFixedThreadPool(2);

      Generun r1 = new Generun();

      executorService.submit(r1);

      Thread.sleep(100);

      executorService.submit(r1);

      executorService.shutdown();


   }

}


class Generun implements Runnable {

   int nextInt = -1;

   int bound = 1000;


   @Override

   public void run() {

      int temp = nextInt;


      nextInt = new Random().nextInt(bound);


      if (temp == nextInt) {

         do {

            nextInt = new Random().nextInt(bound);

         } while (temp == nextInt);

      }

      System.out.println(nextInt);

   }

}


查看完整回答
反對 回復(fù) 2024-01-05
  • 2 回答
  • 0 關(guān)注
  • 156 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯(lián)系客服咨詢優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動學(xué)習(xí)伙伴

公眾號

掃描二維碼
關(guān)注慕課網(wǎng)微信公眾號