Thread pool : Thread Pool « 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.JDK 6
25.JNDI LDAP
26.JPA
27.JSP
28.JSTL
29.Language Basics
30.Network Protocol
31.PDF RTF
32.Reflection
33.Regular Expressions
34.Scripting
35.Security
36.Servlets
37.Spring
38.Swing Components
39.Swing JFC
40.SWT JFace Eclipse
41.Threads
42.Tiny Application
43.Velocity
44.Web Services SOA
45.XML
Java » Threads » Thread PoolScreenshots 
Thread pool
    
/*
Java Threads, 3rd Edition
By Scott Oaks, Henry Wong
3rd Edition September 2004 
ISBN: 0-596-00782-5

*/

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

public class CreateTest extends Thread {
  static AtomicInteger nCalls;

  static int target = 0;

  static boolean done = false;

  static Object lock = new Object();

  public static void main(String[] args) {
    target = 10000;
    doTestCreate();
    doTestPool(8);
    doTestLoop();

    target = Integer.parseInt(args[0]);
    cleanGC();
    Timestamp createTS = new Timestamp(TimeUnit.MICROSECONDS);
    doTestCreate();
    createTS.stop();
    System.out.println("Create thread test took " + createTS);

    cleanGC();
    Timestamp pool8TS = new Timestamp(TimeUnit.MICROSECONDS);
    doTestPool(8);
    pool8TS.stop();
    System.out.println("Pool test (8 threads) took " + pool8TS);

    cleanGC();
    Timestamp poolTS = new Timestamp(TimeUnit.MICROSECONDS);
    doTestPool(1);
    poolTS.stop();
    System.out.println("Pool test (1 thread) took " + poolTS);

    cleanGC();
    Timestamp loopTS = new Timestamp(TimeUnit.MICROSECONDS);
    doTestLoop();
    loopTS.stop();
    System.out.println("Loop test took " + loopTS);

    double d = ((double) (createTS.elapsedTime() - loopTS.elapsedTime()))
        / target;
    System.out.println("Creating a thread took " + d + " " + loopTS.units()
        " per thread");

    d = ((double) (createTS.elapsedTime() - poolTS.elapsedTime())) / target;
    System.out.println("Using a thread pool (1 thread) saved  " + d + " "
        + loopTS.units() " per task");

    d = ((double) (createTS.elapsedTime() - pool8TS.elapsedTime()))
        / target;
    System.out.println("Using a thread pool (8 threads) saved  " + d + " "
        + loopTS.units() " per task");

    d = ((double) (poolTS.elapsedTime() - loopTS.elapsedTime())) / target;
    System.out.println("Thread pool overhead (1 thread) is " + d + " "
        + loopTS.units() " per task");

    d = ((double) (pool8TS.elapsedTime() - loopTS.elapsedTime())) / target;
    System.out.println("Thread pool overhead (8 threads) is " + d + " "
        + loopTS.units() " per task");
  }

  static void doTestLoop() {
    nCalls = new AtomicInteger(0);
    done = false;
    for (int i = 0; i < target; i++)
      work();
    synchronized (lock) {
      while (!done)
        try {
          lock.wait();
        catch (Exception e) {
        }
    }
  }

  static void doTestCreate() {
    done = false;
    nCalls = new AtomicInteger(0);
    for (int i = 0; i < target; i++) {
      Thread t = new CreateTest();
      t.start();
    }
    synchronized (lock) {
      while (!done)
        try {
          lock.wait();
        catch (Exception e) {
        }
    }
  }

static void doTestPool(int nThreads) {
  done = false;
  nCalls = new AtomicInteger(0);
        ThreadPoolExecutor tpe = new ThreadPoolExecutor(nThreads, nThreads,
          50000L, TimeUnit.MILLISECONDS,
          new LinkedBlockingQueue<Runnable>());
        Runnable r = new CreateTest();
        for (int i = 0; i < target; i++) {
            tpe.execute(r);
        }
        tpe.shutdown();
        try {
            tpe.awaitTermination(10000000L, TimeUnit.SECONDS);
        catch (Exception e) {}
    }  public void run() {
    work();
  }

  public static void work() {
    int n = nCalls.incrementAndGet();
    if (n == target) {
      synchronized (lock) {
        done = true;
        lock.notify();
      }
    }
  }

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


           
         
    
    
    
  
Related examples in the same category
1.Defining a thread for a thread poolDefining a thread for a thread pool
2.Thread pool demo
3.Thread Pools 1
4.Thread Pools 2Thread Pools 2
5.Thread Pool 2Thread Pool 2
6.Thread Pool TestThread Pool Test
7.JDK1.5 provides a mechanism to create a pool a scheduled task
8.Very basic implementation of a thread poolVery basic implementation of a thread pool
9.Thread Cache
10.Simple pool of ThreadsSimple pool of Threads
11.Worker thread pool
12.Create a new thread for the thread pool. The create thread will be a daemon thread.
13.Simple thread pool. A task is executed by obtaining a thread from the poolSimple thread pool. A task is executed by obtaining a thread from the pool
14.Simple object pool. Based on ThreadPool and few other classes
15.A utility class that receives a collection of tasks to execute internally and then distributes the tasks among a thread pool.
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.