Thread pool : Thread Pool « Threads « Java

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. Email
14. Event
15. File Input Output
16. Game
17. Generics
18. Hibernate
19. I18N
20. J2EE
21. J2ME
22. JDK 6
23. JSP
24. JSTL
25. Language Basics
26. Network Protocol
27. PDF RTF
28. Reflection
29. Regular Expressions
30. Scripting
31. Security
32. Servlets
33. Spring
34. Swing Components
35. Swing JFC
36. SWT JFace Eclipse
37. Threads
38. Tiny Application
39. Velocity
40. Web Services SOA
41. XML
Java Tutorial
Java Source Code / Java Documentation
Java Open Source
Jar File Download
Java Articles
Java Products
Java by API
C# / C Sharp
C# / CSharp Tutorial
ASP.Net
JavaScript DHTML
JavaScript Tutorial
JavaScript Reference
HTML / CSS
HTML CSS Reference
C / ANSI-C
C Tutorial
C++
C++ Tutorial
Ruby
PHP
Python
SQL Server / T-SQL
SQL Server / T-SQL Tutorial
Oracle PL / SQL
Oracle PL/SQL Tutorial
PostgreSQL
SQL / MySQL
MySQL Tutorial
VB.Net
VB.Net Tutorial
Flash / Flex / ActionScript
VBA / Excel / Access / Word
Microsoft Office PowerPoint 2007 Tutorial
Microsoft Office Excel 2007 Tutorial
Microsoft Office Word 2007 Tutorial
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
w__ww___.j__a_va2s._c__o_m | Contact Us
Copyright 2003 - 08 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.