簡體   English   中英

Java 中的多線程與 3 個線程

[英]Multithreading in Java with 3 threads

我正在嘗試創建以下程序的多線程程序來計算 Pi。 我的簡單程序是:

  import java.util.Random;

  public class MonteCarloPi {
    static Random generator = new Random( System.currentTimeMillis() );
    /**
     * @param args
     */
    public static void main(String[] args) {

            long N = 10000000;
            if ( args.length > 1 )
                    N = Long.parseLong( args[0] );

            double PI = myPiCompute(N);

            System.out.println( N + " random samples yield an approximation of Pi = " + PI);

    }


    public static double myPiCompute (long N){
        long Nc = 0;
            for ( long i=0; i<N; i++ ) {
                    float x = generator.nextFloat()*2 - 1; // random float in [-1,1]
                    float y = generator.nextFloat()*2 - 1; // random float in [-1,1]
                    if ( x*x + y*y <= 1.0f )
                            Nc += 1;
            }

            double PI = 4.0*Nc/N;
            return PI;
    }

}

現在,我正在嘗試使其成為多線程(至少 3 個線程)。 所以我正在執行以下操作,但出現錯誤。

 import java.util.Random;

   class MyThread implements Runnable {
    /**
     * @param args
     */
    static Random generator = new Random( System.currentTimeMillis() );
        public void run() {
            System.out.println("this thread is running ... ");
            long N = 10000000;
            if ( args.length > 1 )
                    N = Long.parseLong( args[0] );
            double PI = myPiCompute(N);

            System.out.println( N + " random samples yield an approximation of Pi = " + PI);

        }
    }


    public static double myPiCompute (long N){
        long Nc = 0;
            for ( long i=0; i<N; i++ ) {
                    float x = generator.nextFloat()*2 - 1; // random float in [-1,1]
                    float y = generator.nextFloat()*2 - 1; // random float in [-1,1]
                    if ( x*x + y*y <= 1.0f )
                            Nc += 1;
            }

            double PI = 4.0*Nc/N;
            return PI;
    }




public class MonteCarloPi {

        public static void main(String[] args) {

                MyThread myObject = new MyThread();
                Thread thr1 = new Thread(myObject);
                thr1.start();

                Thread thr2 = new Thread(myObject);
                thr2.start();

                Thread thr3 = new Thread(myObject);
                thr3.start();
    }

  }

我收到以下錯誤。

     MonteCarloPiPar.java:19: error: class, interface, or enum expected
    public static double myPiCompute (long N){
                  ^
    MonteCarloPiPar.java:21: error: class, interface, or enum expected
            for ( long i=0; i<N; i++ ) {
            ^
    MonteCarloPiPar.java:21: error: class, interface, or enum expected
            for ( long i=0; i<N; i++ ) {
                            ^
    MonteCarloPiPar.java:21: error: class, interface, or enum expected
            for ( long i=0; i<N; i++ ) {
                                 ^
     MonteCarloPiPar.java:23: error: class, interface, or enum expected
                    float y = generator.nextFloat()*2 - 1; // random float in [-1,1]
                    ^
    MonteCarloPiPar.java:24: error: class, interface, or enum expected
                    if ( x*x + y*y <= 1.0f )
                    ^
     MonteCarloPiPar.java:26: error: class, interface, or enum expected
            }
            ^
    MonteCarloPiPar.java:29: error: class, interface, or enum expected
            return PI;
            ^
    MonteCarloPiPar.java:30: error: class, interface, or enum expected
    }
    ^
   9 errors

我正在嘗試使用多線程進行游戲。 任何人都可以指出我做錯了什么。

在錯誤拋出線上方有一個額外的右大括號。

這里:

class MyThread implements Runnable {
    public void run() {
        System.out.println("this thread is running ... ");
        long N = 10000000;
        if ( args.length > 1 )
                N = Long.parseLong( args[0] );
        double PI = myPiCompute(N);

        System.out.println( N + " random samples yield an approximation of Pi = " + PI);

    }
}  // <<< *********

擺脫它,然后刪除這個問題。 將來,發生這種情況時,請計算您的左括號和右括號。

你的“myPiCompute”函數不在一個類中。

暫無
暫無

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

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