Java Thread Performance: AtomicTest : Collections Threads « Threads « Java






Java Thread Performance: AtomicTest

   
/*
Java Threads, 3rd Edition
By Scott Oaks, Henry Wong
3rd Edition September 2004 
ISBN: 0-596-00782-5

*/

import java.util.*;
import java.util.concurrent.*;
import java.util.concurrent.atomic.*;

public class AtomicTest {
  static int nLoops;

  static int nThreads;

  public static void main(String[] args) {

    nLoops = 10000;
    nThreads = 1;
    doTest(new AtomicRunnable());
    doTest(new SyncRunnable());
    nLoops = Integer.parseInt(args[0]);
    nThreads = Integer.parseInt(args[1]);

    System.out.println("Starting atomic test");
    cleanGC();
    Timestamp atomicTS = new Timestamp();
    doTest(new AtomicRunnable());
    atomicTS.stop();
    System.out.println("Atomic took " + atomicTS);

    System.out.println("Starting sync test");
    cleanGC();
    Timestamp syncTS = new Timestamp();
    doTest(new SyncRunnable());
    syncTS.stop();
    System.out.println("Local sync took " + syncTS);

    double d = ((double) (syncTS.elapsedTime() - atomicTS.elapsedTime()))
        / (nLoops * nThreads);
    System.out.println("Atomic operation saves " + d + " " + syncTS.units()
        + " per call");
  }

  static void cleanGC() {
    System.gc();
    System.runFinalization();
    System.gc();
  }

  static class AtomicRunnable implements Runnable {
    AtomicInteger ai = new AtomicInteger(1);

    public void run() {
      for (int i = 0; i < nLoops; i++)
        ai.incrementAndGet();
    }
  }

  static class SyncRunnable implements Runnable {
    int testVar;

    synchronized void incrVar() {
      testVar++;
    }

    public void run() {
      for (int i = 0; i < nLoops; i++)
        incrVar();
    }
  }

  static void doTest(Runnable r) {
    Thread threads[] = new Thread[nThreads];
    for (int i = 0; i < nThreads; i++) {
      threads[i] = new Thread(r);
    }
    for (int i = 0; i < nThreads; i++) {
      threads[i].start();
    }
    for (int i = 0; i < nThreads; i++) {
      try {
        threads[i].join();
      } catch (InterruptedException ie) {
      }
    }
  }
}

class Timestamp {

  private long startTime;

  private long stopTime;

  private boolean stopped = false;

  private TimeUnit ts;

  public Timestamp() {
    this(TimeUnit.NANOSECONDS);
  }

  public Timestamp(TimeUnit ts) {
    this.ts = ts;
    start();
  }

  public void start() {
    startTime = System.nanoTime();
    stopped = false;
  }

  public void stop() {
    stopTime = System.nanoTime();
    stopped = true;
  }

  public long elapsedTime() {
    if (!stopped)
      throw new IllegalStateException("Timestamp not stopped");
    return ts.convert(stopTime - startTime, TimeUnit.NANOSECONDS);
  }

  public String toString() {
    try {
      return elapsedTime() + " " + ts;
    } catch (IllegalStateException ise) {
      return "Timestamp (not stopped)";
    }
  }

  public String units() {
    return ts.toString();
  }
}
           
         
    
    
  








Related examples in the same category

1.Java 1.5 (5.0) new features: PriorityQueueJava 1.5 (5.0) new features: PriorityQueue
2.Safe list copySafe list copy
3.Safe vector copySafe vector copy
4.Safe collection operationSafe collection operation
5.Java 1.5 (5.0) new feature: collection and thread
6.Java Thread Performance: Collection Test
7.Rhyming WordsRhyming Words
8.Communicate between threads using a Queue
9.Using a Thread-Local Variable
10.A work queue is used to coordinate work between a producer and a set of worker threads.
11.Return a value from a thread.
12.A multithreaded queue used for implementing producer-consumer style threading patternsA multithreaded queue used for implementing producer-consumer style threading patterns
13.Customized java.util.ArrayList: operate in a multithreaded environment where the large majority of method calls are read-only, instead of structural changes.
14.Customized java.util.HashMap: operate in a multithreaded environment where the large majority of method calls are read-only, instead of structural changes.
15.Customized java.util.TreeMap: operate in a multithreaded environment where the large majority of method calls are read-only, instead of structural changes.
16.Current set