亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

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

如何打印斐波那契數,多線程?

如何打印斐波那契數,多線程?

慕姐8265434 2021-12-22 20:27:17
任務是編寫一個程序來創建和啟動兩個線程 ThreadFibonacci 和 ThreadOutput。ThreadFiobnacci 應該計算斐波那契數并將結果放入其靜態公共變量中。ThreadOutput 應該輸出斐波那契數并且 ThreadOutput 必須是一個守護線程。你必須讓線程只寫出每個斐波那契數。我不知道如何完成任務的最后一部分。您只能使用 sleep、interrupt、volatile 和 join。這是我嘗試過的:import java.util.Scanner;public class Zadatak2{  public static void main(String[] args){    Scanner reader = new Scanner(System.in);    System.out.println("Enter a number: ");    int n = reader.nextInt();    Thread threadFibonaci = new Thread(new ThreadFibonaci(n));    Thread threadOutput = new ThreadOutput();    threadFibonaci.start();    threadOutput.start();  }}class ThreadFibonaci implements Runnable{  public static volatile long fn;  private int n;  public ThreadFibonaci(int n){    this.n = n;  }  public void run(){    long f0 = 0;    fn = f0;    try{      Thread.sleep(500);    }catch(Exception e){      e.printStackTrace();    }    long f1 = 1;    fn = f1;    try{      Thread.sleep(500);    }catch(Exception e){      e.printStackTrace();    }    for(int i=0; i<n; i++){      fn = f0 + f1;      f0 = f1;      f1 = fn;      try{        Thread.sleep(500);      }catch(Exception e){        e.printStackTrace();      }    }  }}class ThreadOutput extends Thread{  public ThreadOutput(){    setDaemon(true);  }  public void run(){    while(true){      System.out.println(ThreadFibonaci.fn);      try{        Thread.sleep(500);      }catch(Exception e){        e.printStackTrace();      }    }  }}
查看完整描述

2 回答

?
米琪卡哇伊

TA貢獻1998條經驗 獲得超6個贊

您需要再使用一個volatile變量來存儲當前數字是否已打印的標志


class ThreadFibonaci implements Runnable{


  public static volatile long fn;

  public static volatile boolean printed = false;

  private int n;


  public ThreadFibonaci(int n){

    this.n = n;

  }


  public void run(){

    long f0 = 0;

    fn = f0;

    while (!printed) {

      try{

        Thread.sleep(500);

      }catch(Exception e){

        e.printStackTrace();

      }

    }

    long f1 = 1;

    fn = f1;

    printed = false;

    while (!printed) {

      try{

        Thread.sleep(500);

      }catch(Exception e){

        e.printStackTrace();

      }

    }

    for(int i=0; i<n; i++){

      fn = f0 + f1;

      f0 = f1;

      f1 = fn;

      printed = false;

      while (!printed) {

        try{

          Thread.sleep(500);

        }catch(Exception e){

          e.printStackTrace();

        }

      }

    }

 }

}


class ThreadOutput extends Thread{


 public ThreadOutput(){

   setDaemon(true);

 }


 public void run(){

   while(true){

     while (ThreadFibonaci.printed) {

       try{

         Thread.sleep(500);

        }catch(Exception e){

          e.printStackTrace();

        }

     }

     System.out.println(ThreadFibonaci.fn);

     ThreadFibonaci.printed = true;

  }

 }

}


查看完整回答
反對 回復 2021-12-22
?
Helenr

TA貢獻1780條經驗 獲得超4個贊

這使用單個volatile字段來保存值。當值為0新值時可以發布,當值為負時,它充當毒丸,停止打印線程。


class A {

    static volatile long value = 0;

    static void publish(long x) {

        while (value > 0) ;

        value = x;

    }

    static long next() {

        while (value == 0) ;

        long ret = value;

        if (ret > 0) value = 0;

        return ret;

    }

    public static void main(String[] args) {

        System.out.println("Enter a number: ");

        int n = new java.util.Scanner(System.in).nextInt();

        new Thread(() -> {

            long a = 1; publish(a);

            long b = 1; publish(b);

            for (int i = 2; i < n; i++) {

                long c = a + b; publish(c);

                a = b; b = c;

            }

            publish(-1); // poison pill

        }).start();

        new Thread(() -> {

            for (; ; ) {

                long value = next();

                if (value < 0) break;

                System.out.println(value);

            }

        }).start();

    }

}


查看完整回答
反對 回復 2021-12-22
  • 2 回答
  • 0 關注
  • 224 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號