2

基本的には、次の2と3の倍数をこのように順番に出力することです

 2       3       4       6       6       9       8       12      10    = this is the output
(2*1=2) (3*1=3) (2*2=4) (3*2=6) (2*3=6) (3*3=9) (2*4=8) (3*4=12) (2*5=10) = just a guide

これまでのコードは次のとおりです。順番に表示するのに問題があります。wait と notify を使用してみましたが、めちゃくちゃです。これまでのところ、これは機能しています。

public class Main {

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

        final Thread mulof2 = new Thread(){
            public void run() {
                for (int i = 1; i <= 10; i++) {
                    int n = 2;
                    int result = n * i;
                    System.out.print(result + " ");
                }
            }
        };
        Thread mulof3 = new Thread(){
            public void run() {
                for (int i = 1; i <= 10; i++) {
                    int n = 3;
                    int result = n * i;
                    System.out.print(result + " ");
                }
            }
        };
        mulof2.start();
        mulof3.start();

    }

}
4

6 に答える 6

2

Java 7 では、最初の選択肢はPhaser. で作成されたインスタンスが 1 つだけ必要ですnew Phaser(1)arrive調整に必要なメソッドはとの 2 つだけですawaitAdvance

于 2012-07-29T14:30:59.507 に答える
1

スレッドの概念を使用した Java の乗算表

public class Multiplication extends Thread {

     public void run() {
         for (int i = 1; i < 10; i++) {
             int n = 2;
             int result = n * i;
             System.out.print(i+"*"+n+"="+result+"\n");
         }
     }


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

        Multiplication mul=new Multiplication();
        mul.start();

        }

}
于 2015-03-27T05:48:11.243 に答える
0

wait()そしてnotify()、一般的に低レベルであり、使用するには複雑すぎます。のようなより高レベルの抽象化を使用してみてくださいSemaphore

セマフォインスタンスのペアを作成できます。1つは2の次の倍数の印刷を許可し、もう1つは3の次の倍数の印刷を許可します。次の2の倍数が印刷されると、スレッドは印刷を許可する必要があります。次の3の倍数、およびその逆。

もちろん、セマフォの許可の初期数は、2の倍数のセマフォの場合は1、他のセマフォの場合は0でなければなりません。

于 2012-07-29T13:39:20.943 に答える
0

以下は、望ましい結果が得られるコードです。

パブリック クラス メイン {

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

    final Object lock1 = new Object();
    final Object lock2 = new Object();

    final Thread mulof2 = new Thread(){
        public void run() {
            for (int i = 1; i <= 10; i++) {
                synchronized (lock1) {
                    synchronized (lock2) {
                        lock2.notify();
                        int n = 2;
                        int result = n * i;
                       printResult(result);
                    }
                    try {
                        lock1.wait();
                    } catch (InterruptedException e) {
                        // TODO Auto-generated catch block
                        e.printStackTrace();
                    }
                }                    
            }
        }
    };
    Thread mulof3 = new Thread(){
        public void run() {
            for (int i = 1; i <= 10; i++) {
                synchronized (lock2) {
                    synchronized (lock1) {
                         lock1.notify();
                         int n = 3;
                         int result = n * i;
                         printResult(result);
                    }
                    try {
                        lock2.wait();
                    } catch (InterruptedException e) {
                        // TODO Auto-generated catch block
                        e.printStackTrace();
                    }
                }

            }
        }
    };
    mulof2.start();
    mulof3.start();

}

static void printResult(int result)
{
    try {
        // Sleep a random length of time from 1-2s
         System.out.print(result + " ");
        Thread.sleep(new Random().nextInt(1000) + 1000);
    } catch (InterruptedException e) {
        e.printStackTrace();
    }       
}

}

于 2013-11-14T12:27:07.877 に答える
0

簡単な変更で、必要なシーケンスを取得できます。

他の人が指摘したように、セマフォを宣言する必要がありますprivate Semaphore semaphore;。次に、次のようにどのスレッドを実行する必要があるかを示す別の変数を宣言しますprivate int threadToExecute;

semaphore.acquire();次のステップは、スレッド内でとの間のコードを実行することですsemaphore.release();

スレッド 2:

try{
semaphore.acquire();
if(threadToExecute ==2)
  semaphore.release();

//write your multiply by 2 code here
threadToExecute = 3;

semaphore.release();
}catch(Exception e){
//exceptions
}

これにより、出力がうまく同期されます。

于 2012-07-29T14:14:20.233 に答える
0

計算中に出力する代わりに、結果を文字列に集約してから、両方の文字列を順番に出力できます。もちろん、スレッドに参加した後。

于 2012-07-29T13:24:56.487 に答える