簡體   English   中英

每次迭代后停止第一個線程,以便第二個線程可以產生生成的隨機數的階乘

[英]halt the 1st thread after each iteration so the 2nd thread can produce the factorial of that generated Random number

在這里,我想生成隨機生成的數字的階乘。

import java.lang.Math;
public class Assignment1 {
   int random = 1;
   int fact = 1;
   public void RandomGenrator(){
       synchronized(this){
            for(int k=0; k<5;k++){
                random = (int)(Math.random()*10);
                System.out.println("Random Number --> "+random);
                notifyAll();
            }
       }
       
   }

   public void Factorial(){
       synchronized(this){
        if(random == 0){
            System.out.println("Factorial of "+random+" is "+1);
        }else{
            for(int j=1; j<=random; j++){
                fact *=j;
            }
            System.out.println("Factorial of "+random+" is "+fact);
        }
        notify();
       }
       
   }
   public static void main(String[] args) {
       Assignment1 as = new Assignment1();
       Thread t1 = new Thread(new Runnable(){
           public void run(){
               as.RandomGenrator();
           }
       });

       Thread t2 = new Thread(new Runnable(){
          public void run(){
              as.Factorial();
          } 
       });

       t1.start();
       t2.start();
   }
}
This produces output like this
Random Number --> 2
Random Number --> 6 
Random Number --> 4 
Random Number --> 8 
Random Number --> 4 
Factorial of 4 is 24
but I want 
Random Number --> 2
Factorial of 2 is 2
Random Number --> 6 
Factorial of 6 is 720
Random Number --> 4 
Factorial of 4 is 24
Random Number --> 8 
Factorial of 8 is 5760
Random Number --> 4
Factorial of 4 is 24

我知道這可以通過簡單的 for 循環來實現,if else 結構但我想使用線程來實現這一點,那么如何在每次迭代后停止第一個線程,以便第二個線程可以產生生成的隨機數的階乘

您的代碼沒有使用任何wait ,因此沒有線程會等待另一個線程。 您可以使用boolean變量來指示誰在等待誰。 在這里,如果flag為真,則階乘正在等待隨機生成器生成一個數字。 如果flag為 false,則隨機生成器正在等待階乘計算。

boolean flag = true; // initially, we wait for the random generator to generate first

public void RandomGenrator(){
    for (int i = 0 ; i < 5 ; i++) {
        synchronized (this) {
            while (!flag) {
                try {
                    wait();
                } catch (InterruptedException e) {
                    System.out.println("interrupted");
                    Thread.currentThread().interrupt();
                }
            }
            random = (int) (Math.random() * 10);
            System.out.println("Random Number --> " + random);
            flag = false; // now waiting for the factorial to compute!
            notify();
        }
    }
}

public void Factorial(){
    for (int i = 0 ; i < 5 ; i++) {
        synchronized (this) {
            while (flag) {
                try {
                    wait();
                } catch (InterruptedException e) {
                    System.out.println("interrupted");
                    Thread.currentThread().interrupt();
                }
            }
            if (random == 0) {
                System.out.println("Factorial of " + random + " is " + 1);
            } else {
                fact = 1;
                for (int j = 1; j <= random; j++) {
                    fact *= j;
                }
                System.out.println("Factorial of " + random + " is " + fact);
            }
            flag = true; // now waiting for the random generator to generate a new number!
            notify();
        }
    }
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM