-1

私は HiveMQ を使用しており、各スレッドに 1 つずつ 2 つのクライアントがあり、これら 2 つのスレッドを開始するメイン スレッドがあります。送信 (一方のスレッドの場合) と受信 (もう一方のスレッドの場合) の両方の時間を計るために使用してきたSystem.nanoTime()ので、値を合計して、指定された数のメッセージを送受信する合計時間を取得できます。1 つのスレッドが別のスレッドをウェイクアップする必要があるのとほぼ同時にタイマーが開始されるように、Java でwait()同期ブロックを使用しました。notify()送受信の最終時間は、約 0 ~ 350 ミリ秒と異なるようですプログラムを実行すると、15 個のメッセージが自然に送受信されます。コンテキストとして、サーバーとクライアントは、サーバー アドレスとして localhost を使用して同じマシン上で実行されています。とにかく、スレッド全体でより正確な時間 (変化の少ない) を取得できる方法はありますか? できるだけ精度を上げたい。

サブスクライバー (送信側クライアント) のコード:

scheduler.waitToReceive();  // makes the SubThread wait until the PubThread is ready to send a message 

            System.out.println("Timer started");
            startTime = System.nanoTime();

            for (int i = 1; i <= 15; i++) {  
                Mqtt5Publish receivedMessage = receivingClient1.receive(MESSAGEWAITTIME,TimeUnit.SECONDS).get(); // receives the message using the "publishes" instance                                                                         // .get() returns the object if available or throws a NoSuchElementException 
                PubSubUtility.convertMessage(receivedMessage);  // Converts a Mqtt5Publish instance to string and prints 
            }   
            endTime = System.nanoTime();

パブリッシャー (パブリッシング クライアント) のコード:

readyToSend = true; 
        scheduler.notifyStartReceive();  // notifies SubThread it can starts receiving messages
        startTime = System.nanoTime();

            for (int i = 1; i <= 15; i++) { 
                 publisher.publishWith()    
                 .topic(publisherTopic)   // publishes to the specified topic
                 .qos(MqttQos.EXACTLY_ONCE)  // sets the quality of service to 2 
                 .payload(convertedMessage)  // the contents of the message 
                 .send();
            }           
        endTime = System.nanoTime();
4

1 に答える 1