Java Thread Performance: Collection Test : Collections Threads « Threads « Java

Home
Java
1.2D Graphics GUI
2.3D
3.Advanced Graphics
4.Ant
5.Apache Common
6.Chart
7.Class
8.Collections Data Structure
9.Data Type
10.Database SQL JDBC
11.Design Pattern
12.Development Class
13.EJB3
14.Email
15.Event
16.File Input Output
17.Game
18.Generics
19.GWT
20.Hibernate
21.I18N
22.J2EE
23.J2ME
24.JavaFX
25.JDK 6
26.JDK 7
27.JNDI LDAP
28.JPA
29.JSP
30.JSTL
31.Language Basics
32.Network Protocol
33.PDF RTF
34.Reflection
35.Regular Expressions
36.Scripting
37.Security
38.Servlets
39.Spring
40.Swing Components
41.Swing JFC
42.SWT JFace Eclipse
43.Threads
44.Tiny Application
45.Velocity
46.Web Services SOA
47.XML
Java » Threads » Collections Threads 




Java Thread Performance: Collection Test
   
/*
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 CollectionTest {
  static int nLoops;

  public static void main(String[] args) {

    nLoops = 10000;
    doTest(new Vector());
    doTest(new ArrayList());
    doTest(Collections.synchronizedList(new ArrayList()));
    nLoops = Integer.parseInt(args[0]);

    System.out.println("Starting synchronized vector test");
    cleanGC();
    Timestamp syncTS = new Timestamp();
    doTest(new Vector());
    syncTS.stop();
    System.out.println("Synchronized vector took " + syncTS);

    System.out.println("Starting unsynchronized vector test");
    cleanGC();
    Timestamp unsyncTS = new Timestamp();
    unsyncTS.stop();
    System.out.println("Unsynchronized vector took " + unsyncTS);

    double d = ((double) (syncTS.elapsedTime() - unsyncTS.elapsedTime()))
        / nLoops;
    System.out.println("Unsynchronized operation saves " + d + " "
        + syncTS.units() " per call");

    System.out.println("Starting synchronized array list test");
    cleanGC();
    syncTS = new Timestamp();
    doTest(Collections.synchronizedList(new ArrayList()));
    syncTS.stop();
    System.out.println("Synchronized array list took " + syncTS);

    System.out.println("Starting unsynchronized array list test");
    cleanGC();
    unsyncTS = new Timestamp();
    doTest(new ArrayList());
    unsyncTS.stop();
    System.out.println("Unsynchronized aray list took " + unsyncTS);

    d = ((double) (syncTS.elapsedTime() - unsyncTS.elapsedTime())) / nLoops;
    System.out.println("Unsynchronized operation saves " + d + " "
        + syncTS.units() " per call");
  }

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

  static void doTest(List l) {
    Integer n = new Integer(0);
    for (int i = 0; i < nLoops; i++)
      l.add(n);
  }
}
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: AtomicTest
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
java2s.com  | Contact Us | Privacy Policy
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.