http://www.ibm.com/developerworks/library/j-jtp0730/index.html
线程池的原理:
1. 内部维护若干个线程,这些线程一直保持运行/阻塞状态;
2. 维护一个队列,通过锁的方式让这些线程从队列中取任务;
3. 如果队列没有任务,则阻塞当前请求任务的线程;
4. 当有新的任务加入到任务队列时,唤醒与锁绑定的等待集中的线程
这就是线程池最基本的原理!
package thread; import java.util.LinkedList; public class WorkQueue { private final int nThreads; private final PoolWorker[] threads; private final LinkedList queue; public WorkQueue(int nThreads) { this.nThreads = nThreads; threads = new PoolWorker[nThreads]; queue = new LinkedList(); for(int i=0; i<nThreads; i++) { threads[i] = new PoolWorker("worker-" + i); threads[i].start(); System.out.println(threads[i].getName()+" starts to work"); } } //面向接口进行设计,只要是实现Runnable接口的任务都可以被接收 public void execute(Runnable task) { synchronized(queue) { queue.add(task); System.out.println("$$$ current tasks:" + queue.size()); queue.notify(); } } //worker的任务:处理任务队列 Task Queue 中的任务 private class PoolWorker extends Thread { public PoolWorker(String threadName) { super(threadName); } public void run() { Runnable task; for(;;) { synchronized (queue) { while(queue.isEmpty()) { try { System.out.println("there is no task! "+Thread.currentThread().getName() + " is waiting..."); queue.wait(); } catch (InterruptedException ignored) {} } //取出任务 task = (Runnable)queue.removeFirst(); } try{ task.run(); } catch(RuntimeException e) { //log something here } } } } }
package thread; import java.io.File; import java.text.SimpleDateFormat; import java.util.Date; import java.util.Random; /** * 实现Runnable表示此类为任务类 * */ public class FileSaver implements Runnable { private File file; public FileSaver(File file) { this.file = file; } @Override public void run() { try { long mills = new Random().nextInt(10000); Thread.sleep(mills); SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss.SSS"); System.out.println(Thread.currentThread().getName() + ": Write file to disk at " + sdf.format(new Date()) + "--->" + file.getName() + ", takes " + mills + " ms"); } catch (InterruptedException e) { e.printStackTrace(); } } }
package thread; import java.io.File; import java.text.SimpleDateFormat; import java.util.Calendar; import java.util.Random; public class Client { public static void main(String[] args) { //系统启动便初始化工作队列 WorkQueue queue = new WorkQueue(2); //模拟客户端请求 Random rand = new Random(); SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss.SSS"); int i = 1; while(true) { try { Thread.sleep(rand.nextInt(5000)); String fileName = "file"+(i++); System.out.println("One request arrive at " + sdf.format(Calendar.getInstance().getTime()) + ", fileName=" + fileName); File file = new File(fileName); FileSaver saver = new FileSaver(file); queue.execute(saver); } catch (InterruptedException e) { e.printStackTrace(); } } } }
相关推荐
ThreadPools
ThreadPoolWithCameraPreview, 在Android上,使用threadpools提高性能的演示 带相机预览的ThreadPool下载演示应用在 Google Play 上。项目演示了如何使用 HandlerThread s 和 ThreadPool s 在后台线程的android API...
SP6ex1ThreadPools
集约水稻种植下钾肥对土壤钾库和水稻产量的影响,张奇春,王光火,在多年双季稻栽培系统下,为了研究K肥对土壤K库及土壤水稻产量、K平衡和水稻吸K量的影响,在浙江省金华市农科院(ARI,土壤呈碱性�
VC++ 6.0下实现的线程池程序。 用户可以在WorkItem下设置要完成的任务,设置好工作函数,即可运行线程池开始多线程工作。
Java 线程池应用程序这个应用程序是作为我本科课程工作的一部分开发的,该课程称为基于性能的编程模块。 该模块主要关注 Java 中多线程应用程序的安全开发。 该作业专门研究了线程池和同步的概念。...
ThreadPools.jl是一个简单的程序包,它公开了一些模仿Base.Threads.@threads , Base.map和Base.foreach宏和函数。这些宏(和基础API)处理内置函数并非总是很适合的情况: 用户希望远离主线程的一组任务 一组持续...
Power pools are formal groups of Utilities that exchange electricity in order to reduce production costs. The purpose of this paper is to present a framework which assesses the impacts of different ...
自己写好的对象池,可以拿下来直接用,挺简单的哈,为什么要50个字的描述,能说清楚不久得了,希望改进XXXXXXXXXXXXXXXXXXXX
android.support.v4.util.Pools使用的Demo,详情见博客:http://blog.csdn.net/u012810020/article/details/52915922
Memory Pools V1.2 Memory Pools V1.2
Using thread pools 342 Customizing the thread pool dispatcher 354 Using remote threading 371 Appendix 381 Installing MySQL Connector/C 381 Installing WinDDK – Driver Development Kit 384 Setting up a ...
New chapters cover thread performance, using threads with Swing, threads and Collection classes, thread pools, and threads and I/O (traditional, new, and interrupted). Developers who cannot yet ...
Multithreadingincluding the java.util.concurrent library, locks, condition objects, futures, thread pools, thread-safe collections, threads and Swing Collection classescollections framework, ...
Further, you’ll learn about server scalability, asynchronous I/O, and thread pools, and write responsive traditional Windows and Windows Store applications. By the end of the book, you will be able...
This class can preverify methods in program class pools, according to a given configuration.
* Improving performance and scalability using threads, thread pools, and completion ports * Techniques to improve program reliability and performance in all systems * Windows performance-enhancing API...
* Improving performance and scalability using threads, thread pools, and completion ports * Techniques to improve program reliability and performance in all systems * Windows performance-enhancing API...
Further, you'll learn about server scalability, asynchronous I/O, and thread pools, and write responsive traditional Windows and Windows Store applications. By the end of the book, you will be able ...
mem poolsmem poolsmem pools