Multithreaded code
私は、私たちのチームメイトコードのほとんどをベンチマークしようとしているときに、特定のメソッドにかかる時間を測定しようとしているところに取り組んでいLoad and Performance
ます.Client code
Service code
したがって、このパフォーマンス測定のために、私は使用しています-
System.nanoTime();
そして、複数のスレッドを生成し、そのコードにかかる時間を測定しようとしているマルチスレッドコードを持っています。
以下は、任意のコードのパフォーマンスを測定しようとしているサンプル例です-以下のコードでは、測定しようとしています-
beClient.getAttributes method
以下はコードです-
public class BenchMarkTest {
public static void main(String[] args) {
ExecutorService executor = Executors.newFixedThreadPool(5);
try {
for (int i = 0; i < 3 * 5; i++) {
executor.submit(new ThreadTask(i));
}
executor.shutdown();
executor.awaitTermination(Long.MAX_VALUE, TimeUnit.DAYS);
} catch (InterruptedException e) {
}
}
}
以下はRunnableインターフェースを実装するクラスです
class ThreadTask implements Runnable {
private int id;
public static ConcurrentHashMap<Long, AtomicLong> selectHistogram = new ConcurrentHashMap<Long, AtomicLong>();
public ThreadTask(int id) {
this.id = id;
}
@Override
public void run() {
long start = System.nanoTime();
attributes = beClient.getAttributes(columnsList);
long end = System.nanoTime() - start;
final AtomicLong before = selectHistogram.putIfAbsent(end / 1000000L, new AtomicLong(1L));
if (before != null) {
before.incrementAndGet();
}
}
}
測定したいコードが何であれ、通常はそのメソッドのすぐ上に以下の行を置きます
long start = System.nanoTime();
そして、これらの2行は同じ方法の後にありますが、ConcurrentHashMap
long end = System.nanoTime() - start;
final AtomicLong before = selectHistogram.putIfAbsent(end / 1000000L, new AtomicLong(1L));
if (before != null) {
before.incrementAndGet();
}
今日、私は先輩の一人と面会しましたが、彼はincrementAndGet
方法ConcurrentHashMap
はブロッキングコールだと言いました. したがって、スレッドはそこでしばらく待機します。
そして彼は私にそれを作るように頼んだAsynchronous call
。
その非同期呼び出しを行う可能性はありますか?
すべてのクライアント コードとサービス コードで各メソッドのパフォーマンスを測定するため、通常は各メソッドの前後に配置するのと同じ上記の 3 行を使用して、これらのメソッドのパフォーマンスを測定します。プログラムが終了したら、それらのマップの結果を出力します。
だから今私はそれを作ることを考えていAsynchronous call
ますか?誰でも私がそれをするのを手伝ってくれますか?
基本的に、各スレッドが待機してブロックされないように、特定のメソッドのパフォーマンスを非同期で測定しようとしています。
を使用してこれを行うことができると思いますFutures
。誰かがそれに関連する例を提供できますか?
助けてくれてありがとう。