- 浏览: 162344 次
文章分类
最新评论
-
sydneytsao2:
gfs球棒gsd
Java设计模式——六大原则 -
涛涛饭:
memoryisking 写道可以看看这篇文章,构建一个简单的 ...
Java线程池使用说明 -
a492846462:
Java线程池使用说明 -
sprita1:
资源很好很强大,对楼主膜拜中.....
Java线程池使用说明 -
memoryisking:
可以看看这篇文章,构建一个简单的线程池:http://www. ...
Java线程池使用说明
为什么要建立线程池?
在多线程项目中,如果建立的线程过多,反而可能导致运行速度大大减慢,这是由于线程建立所花费的时间和资源都比较多。
所以我们在多线程中必须很好地来管理线程, 在很好利用多线程能“同步工作”的好处之外,更有效地提高程序运行速度。
线程池是什么?
线程池是指具有固定数量的线程组成的一种组件。这些线程用来循环执行多个应用逻辑。
怎么建立线程池?
线程池主要包括4个部分,它们是:
1. 线程管理
主要是用来建立,启动,销毁工作线程和把工作任务加入工作线程。
2. 工作线程
它是真正的线程类,运行工作任务。
3. 工作队列
它是用来封装线程的容器。
4. 工作任务
它是实现应用逻辑的具体类。
流程图:
线程管理类:
- <SPAN style="COLOR: #3366ff">import java.util.ArrayList;
- import java.util.List;
- import java.util.Queue;
- import java.util.concurrent.ConcurrentLinkedQueue;
- /**
- * ThreadPoolManager.java
- *
- */
- /**
- * the thread pool manager, is responsible for starting and stopping the work thread.
- *
- * @author gray
- * @version 1.0
- */
- public class ThreadPoolManager {
- private static final int DEFAULT_POOL_SIZE = 4;
- private List<WorkThread> threadPool;
- private Queue<Task> taskQueue;
- private int poolSize;
- public ThreadPoolManager() {
- this(DEFAULT_POOL_SIZE);
- }
- public ThreadPoolManager(int poolSize) {
- if(poolSize <= 0) {
- this.poolSize = DEFAULT_POOL_SIZE;
- }else {
- this.poolSize = poolSize;
- }
- threadPool = new ArrayList<WorkThread>(this.poolSize);
- taskQueue = new ConcurrentLinkedQueue<Task>();
- startup();
- }
- public void startup() {
- System.out.println("start work thread...");
- synchronized(taskQueue) {
- for(int i = 0; i < this.poolSize; i++) {
- WorkThread workThread = new WorkThread(taskQueue);
- threadPool.add(workThread);
- workThread.start();
- }
- }
- }
- public void shutdown() {
- System.out.println("shutdown work thread...");
- synchronized(taskQueue) {
- for(int i = 0; i < this.poolSize; i++) {
- threadPool.get(i).shutdown();
- }
- System.out.println("done...");
- }
- }
- public void addTask(Task task) {
- synchronized(taskQueue) {
- taskQueue.add(task);
- taskQueue.notify();
- }
- }
- }</SPAN>
import java.util.ArrayList;
import java.util.List;
import java.util.Queue;
import java.util.concurrent.ConcurrentLinkedQueue;
/**
* ThreadPoolManager.java
*
*/
/**
* the thread pool manager, is responsible for starting and stopping the work thread.
*
* @author gray
* @version 1.0
*/
public class ThreadPoolManager {
private static final int DEFAULT_POOL_SIZE = 4;
private List<WorkThread> threadPool;
private Queue<Task> taskQueue;
private int poolSize;
public ThreadPoolManager() {
this(DEFAULT_POOL_SIZE);
}
public ThreadPoolManager(int poolSize) {
if(poolSize <= 0) {
this.poolSize = DEFAULT_POOL_SIZE;
}else {
this.poolSize = poolSize;
}
threadPool = new ArrayList<WorkThread>(this.poolSize);
taskQueue = new ConcurrentLinkedQueue<Task>();
startup();
}
public void startup() {
System.out.println("start work thread...");
synchronized(taskQueue) {
for(int i = 0; i < this.poolSize; i++) {
WorkThread workThread = new WorkThread(taskQueue);
threadPool.add(workThread);
workThread.start();
}
}
}
public void shutdown() {
System.out.println("shutdown work thread...");
synchronized(taskQueue) {
for(int i = 0; i < this.poolSize; i++) {
threadPool.get(i).shutdown();
}
System.out.println("done...");
}
}
public void addTask(Task task) {
synchronized(taskQueue) {
taskQueue.add(task);
taskQueue.notify();
}
}
}
工作线程类:
- <SPAN style="COLOR: #3366ff">import java.util.Queue;
- /**
- * WorkThread.java
- *
- */
- /**
- * the work thread used pull the task of task queue, and execute it.
- *
- * @author gray
- * @version 1.0
- */
- public class WorkThread extends Thread {
- private boolean shutdown = false;
- private Queue<Task> queue;
- public WorkThread(Queue<Task> queue) {
- this.queue = queue;
- }
- public void run() {
- while(!shutdown) {
- try {
- Thread.sleep(1000);
- } catch (InterruptedException e1) {
- e1.printStackTrace();
- }
- System.out.println(Thread.currentThread() + " is running...");
- synchronized(queue) {
- if(!queue.isEmpty()) {
- Task task = queue.poll();
- task.execute();
- }else {
- try {
- queue.wait(1000);
- System.out.println(Thread.currentThread() + " wait...");
- }catch(InterruptedException e) {
- }
- }
- }
- }
- }
- public void shutdown() {
- shutdown = true;
- }
- }</SPAN>
import java.util.Queue;
/**
* WorkThread.java
*
*/
/**
* the work thread used pull the task of task queue, and execute it.
*
* @author gray
* @version 1.0
*/
public class WorkThread extends Thread {
private boolean shutdown = false;
private Queue<Task> queue;
public WorkThread(Queue<Task> queue) {
this.queue = queue;
}
public void run() {
while(!shutdown) {
try {
Thread.sleep(1000);
} catch (InterruptedException e1) {
e1.printStackTrace();
}
System.out.println(Thread.currentThread() + " is running...");
synchronized(queue) {
if(!queue.isEmpty()) {
Task task = queue.poll();
task.execute();
}else {
try {
queue.wait(1000);
System.out.println(Thread.currentThread() + " wait...");
}catch(InterruptedException e) {
}
}
}
}
}
public void shutdown() {
shutdown = true;
}
}
工作任务接口:
- <SPAN style="COLOR: #3366ff">/**
- * Task.java
- *
- */
- /**
- * The task want to execute.
- *
- * @author gray
- * @version 1.0
- */
- public interface Task {
- public void execute();
- }</SPAN>
/**
* Task.java
*
*/
/**
* The task want to execute.
*
* @author gray
* @version 1.0
*/
public interface Task {
public void execute();
}
工作任务类:
- <SPAN style="COLOR: #3366ff">/**
- * SimpleTask.java
- *
- */
- /**
- * @author gray
- * @version 1.0
- */
- public class SimpleTask implements Task {
- /* (non-Javadoc)
- * @see Task#execute()
- */
- public void execute() {
- System.out.println(Thread.currentThread());
- }
- }</SPAN>
/**
* SimpleTask.java
*
*/
/**
* @author gray
* @version 1.0
*/
public class SimpleTask implements Task {
/* (non-Javadoc)
* @see Task#execute()
*/
public void execute() {
System.out.println(Thread.currentThread());
}
}
线程池测试类:
- <SPAN style="COLOR: #3366ff">/**
- * ThreadPoolDemo.java
- *
- */
- /**
- * @author gray
- * @version 1.0
- */
- public class ThreadPoolDemo {
- public static void main(String[] args) {
- ThreadPoolManager threadMg = new ThreadPoolManager();
- for(int i = 0; i < 50; i++) {
- threadMg.addTask(new SimpleTask());
- }
- try {
- Thread.sleep(5000);
- } catch (InterruptedException e) {
- e.printStackTrace();
- }
- threadMg.shutdown();
- }
- }</SPAN>
发表评论
-
问题列表
2012-08-15 22:01 7961.如何验证xml文件是否符合规范 2.sql常用操作建存储 ... -
【JDBC】java中常见三种注册驱动方式比较
2012-08-08 09:26 10231.DriverManager.registerDriver( ... -
排序都有哪几种方法
2012-08-03 08:49 1449排序方法一般都就那几 ... -
Java定时器
2012-08-02 14:28 0请说说你对Java定时器的认识,尝试写一个例子来说明。 答:在 ... -
编写一个程序,将d:\java目录下的所有.java文件复制到d:\jad目录下,并将原来文件的扩展名从.java改为.jad
2012-08-02 11:54 2795编写一个程序,将d:\java目录下的所有.java文件复制到 ... -
java中流的详解
2012-08-01 16:50 992java中流的详解 博客分类: j2ee ... -
如何处理大数据量的查询
2012-07-31 14:38 3370J2EE综合:如何处理大数 ... -
大数据量处理
2012-07-31 10:59 825大数据量处理(一) 大数据量的问题是很多面试笔试 ... -
三道面试题
2012-07-31 10:01 1030[讨论] 今天去面试了,有三个题目问倒我了,写出来大家做做! ... -
实现从文件中一次读出一个字符的操作,java实现从文件中一次读出一个字符的操作
2012-07-30 16:41 0实现从文件中一次读出一个字符的操作,java实现从文件中 ... -
Java线程池使用说明
2012-07-30 16:35 59719Java线程池使用说明 一 简介 线程的使用在java中占 ... -
一个简单的线程池
2012-07-30 15:55 994import java.util.LinkedList; ... -
Java多线程:类ThreadPoolExecutor详解
2012-07-30 15:04 1344Java多线程:类ThreadPoolExecutor ... -
Java程序员面试中的多线程问题
2012-07-30 14:42 543很多核心 Java 面试题来 ...
相关推荐
线程池线程池线程池线程池线程池线程池线程池线程池线程池线程池线程池线程池线程池线程池线程池线程池线程池线程池线程池线程池线程池线程池线程池线程池线程池线程池线程池线程池线程池线程池线程池线程池线程池...
corePoolSize:核心池的大小,在创建了线程池后,线程池中的线程数为0,当有任务来之后,就会创建一个线程去执行任务,当线程池中的线程数目达到corePoolSize后,就会把到达的任务放到缓存队列当中; ...
阻塞线程池阻塞线程池阻塞线程池阻塞线程池阻塞线程池阻塞线程池阻塞线程池阻塞线程池阻塞线程池阻塞线程池阻塞线程池阻塞线程池阻塞线程池阻塞线程池阻塞线程池阻塞线程池阻塞线程池阻塞线程池阻塞线程池阻塞线程池...
一、线程池 1、为什么需要使用线程池 1.1 创建/销毁线程伴随着系统开销,过于频繁的创建/销毁线程,会很大程度上影响处理效率。 记创建线程消耗时间T1,执行任务消耗时间T2,销毁线程消耗时间T3,如果T1+T3>T2,那...
VC实现线程池
易语言简易线程池的实现。 ——V雪落有声V原创。转载请保留。前文:。为了能充分理解本篇文章的内容,需要了解的知识如下:。1.事件对象的使用:http://baike.baidu.com/view/751499.htm。2.信号量的使用:...
简单的线程池程序+中文文档 包结构: com.tangkai.threadpool --SimpleThread.java 工作线程 --TestThreadPool.java 程序入口 --ThreadPoolManager.java 线程池管理类
Windows下一个比较完美的线程池实现和示例 本线程池提供了如下功能: 1.能根据任务个数和当前线程的多少在最小/最大线程个数之间自动调整(Vista后的系统有 SetThreadpoolThreadMaximum 等函数有类似功能); 2.能方便...
C#管理线程池的类 /* How to use Thread Classs * * ============== * public ELMService() { InitializeComponent(); etm.ClalThreadPool("EmailThreads", (uint)ApplicationInfo.EmailParsingThreads); ...
线程池
因为本人是个小白,多线程经常用,但是线程池并没有用过,(一听到线程池,总感觉高大上)。但是近期写彩票软件的时候发现,多线程长期操作会导致内容不断的升高直至报错,遂想起了线程池,完善后发现不是一般的叼...
2.然后根据提示运行java命令执行示例程序,观看线程池的运行结果 目标:Java中多线程技术是一个难点,但是也是一个核心技术。因为Java本身就是一个多线程语言。本人目前在给46班讲授Swing的网络编程--使用Swing来...
1.线程池管理器(ThreadPoolManager):用于创建并管理线程池 2.工作线程(WorkThread): 线程池中线程 3.任务接口(Task):每个任务必须实现的接口,以供工作线程调度任务的执行。 4.任务队列:用于存放没有处理的...
以下示例显示如何使用线程池。首先创建 ManualResetEvent 对象,此对象使程序能够知道线程池何时运行完所有的工作项。接着,尝试向线程池添加一个线程。如果添加成功,则添加其余的线程(本例中为 4 个)。然后...
linux线程池创建c实现 linux线程池创建c实现 linux线程池创建c实现 linux线程池创建c实现 linux线程池创建c实现 linux线程池创建c实现
使用Windows自带的线程池功能,比你写的线程池性能好得多
什么是线程池?简单点说,线程池就是有一堆已经创建好了的线程,初始它们都处于空闲等待状态,当有新的任务需要处理的时候,就从这个池子里面取一个空闲等待的线程来处理该任务,当处理完成了就再次把该线程放回池中...
这是一个简单线程池的实现,虽然有很多bug,但是能够简单地实现线程池。
Java开发,Android开发,自己实现线程池,明白线程池的实现机制
本实例采用c3p0作为线程池工具包,讲解了jdbc基本用法,同时给出了Oracle以及mysql增(单插入、批量插入)、删、查、改等功能,可以直接复制使用。