因为学了数据库连接池,左思右想,搞了个线程池举一反三以下是简单实现,不知道是否有不妥package com.lxw.thread;
import java.util.LinkedList;
public class ThreadPool {
private LinkedList<MyThread> list = new LinkedList<MyThread>();
//默认线程池数量
private static final int deflautPoolSize = 10;
public ThreadPool(int Poolsize) {
for (int i = 0; i < Poolsize; i++) {
list.add(new MyThread());
}
}
public ThreadPool() {
this(deflautPoolSize);
}
public synchronized void destoryAllThread() {
for (int i = 0; i < list.size(); i++) {
list.get(i).setStop(true);
}
notifyAll();
}
//获得线程池中的一个可用线程,你传个实现了Runnable接口的对象,调用start方法,会调用这个对象的run方法
public Thread getThread(Runnable r) {
if (list.size() != 0) {
MyThread thread = list.removeFirst();
thread.setRunnable(r);
return thread;
}
throw new RuntimeException("线程池中没有线程可供使用");
}
/*
* 一份特殊的线程,是线程池中可以重复利用的线程
* 一般增强一个类有三种方法:1,成为其子类 2,装饰模式(jdk中的IO流就是) 3,代理模式
* 本例子用的方法为第一种
*/
class MyThread extends Thread {
private Runnable runnable;
private boolean Stop = true;
private boolean isStart = false;
public void setStop(boolean stop) {
Stop = stop;
start();
}
private void setRunnable(Runnable r) {
runnable = r;
}
@Override
public synchronized void start() {
if (!isStart) {
isStart = true;
Stop = false;
super.start();
} else {
notify();
}
}
@Override
public synchronized void run() {
while (!Stop) {
runnable.run();
try {
list.addLast(this);
this.wait();
} catch (InterruptedException e) {
throw new RuntimeException(e);
}
}
}
}
//测试mian方法
public static void main(String[] args) throws InterruptedException {
ThreadPool threadPool = new ThreadPool(3);
Thread t1 = threadPool.getThread(new Testthread(5));
t1.start();
threadPool.getThread(new Testthread(15)).start();
threadPool.getThread(new Testthread(25)).start();
Thread.sleep(5000);
threadPool.getThread(new Testthread(35)).start();
threadPool.getThread(new Testthread(45)).start();
Thread.sleep(3000);
threadPool.getThread(new Testthread(35)).start();
threadPool.getThread(new Testthread(45)).start();
threadPool.getThread(new Testthread(35)).start();
Thread.sleep(3000);
threadPool.destoryAllThread();
}
/*
* 一个普通的线程,测试所用
*/
static class Testthread implements Runnable {
private int count;
public Testthread(int count) {
this.count = count;
}
public Testthread() {
super();
// TODO Auto-generated constructor stub
}
@Override
public void run() {
System.out.println(count++ + "");
System.out.println(count++ + "");
System.out.println(count++ + "");
System.out.println(count++ + "");
System.out.println("threadName:" + Thread.currentThread().getName());
}
}
}
import java.util.LinkedList;
public class ThreadPool {
private LinkedList<MyThread> list = new LinkedList<MyThread>();
//默认线程池数量
private static final int deflautPoolSize = 10;
public ThreadPool(int Poolsize) {
for (int i = 0; i < Poolsize; i++) {
list.add(new MyThread());
}
}
public ThreadPool() {
this(deflautPoolSize);
}
public synchronized void destoryAllThread() {
for (int i = 0; i < list.size(); i++) {
list.get(i).setStop(true);
}
notifyAll();
}
//获得线程池中的一个可用线程,你传个实现了Runnable接口的对象,调用start方法,会调用这个对象的run方法
public Thread getThread(Runnable r) {
if (list.size() != 0) {
MyThread thread = list.removeFirst();
thread.setRunnable(r);
return thread;
}
throw new RuntimeException("线程池中没有线程可供使用");
}
/*
* 一份特殊的线程,是线程池中可以重复利用的线程
* 一般增强一个类有三种方法:1,成为其子类 2,装饰模式(jdk中的IO流就是) 3,代理模式
* 本例子用的方法为第一种
*/
class MyThread extends Thread {
private Runnable runnable;
private boolean Stop = true;
private boolean isStart = false;
public void setStop(boolean stop) {
Stop = stop;
start();
}
private void setRunnable(Runnable r) {
runnable = r;
}
@Override
public synchronized void start() {
if (!isStart) {
isStart = true;
Stop = false;
super.start();
} else {
notify();
}
}
@Override
public synchronized void run() {
while (!Stop) {
runnable.run();
try {
list.addLast(this);
this.wait();
} catch (InterruptedException e) {
throw new RuntimeException(e);
}
}
}
}
//测试mian方法
public static void main(String[] args) throws InterruptedException {
ThreadPool threadPool = new ThreadPool(3);
Thread t1 = threadPool.getThread(new Testthread(5));
t1.start();
threadPool.getThread(new Testthread(15)).start();
threadPool.getThread(new Testthread(25)).start();
Thread.sleep(5000);
threadPool.getThread(new Testthread(35)).start();
threadPool.getThread(new Testthread(45)).start();
Thread.sleep(3000);
threadPool.getThread(new Testthread(35)).start();
threadPool.getThread(new Testthread(45)).start();
threadPool.getThread(new Testthread(35)).start();
Thread.sleep(3000);
threadPool.destoryAllThread();
}
/*
* 一个普通的线程,测试所用
*/
static class Testthread implements Runnable {
private int count;
public Testthread(int count) {
this.count = count;
}
public Testthread() {
super();
// TODO Auto-generated constructor stub
}
@Override
public void run() {
System.out.println(count++ + "");
System.out.println(count++ + "");
System.out.println(count++ + "");
System.out.println(count++ + "");
System.out.println("threadName:" + Thread.currentThread().getName());
}
}
}
解决方案 »
- 讨论JAVA异常处理
- JAVA 线程 sleep()和synchronized 的抛异常问题
- 面试给一个问题问倒了
- Sun 中国技术社区发布JavaChina 2005大会演讲资料
- org.apache.myfaces.custom.fileupload.UploadedFile获得文件路径名(急求)
- 微软的MFC类库是对Windows API函数的封装,Java的JDK中的基础类库是对什么的封装?
- 请问大侠们,如何计算程序的执行时间(毫秒级的)!!谢谢了
- 如何在网页源码中找到想要的部分?
- 如何解决数值计算中的误差
- jbuider 怎样调试beans
- 向Excel中写数据问题
- Eclipse 的智能提示太悲剧了
假设,若干次以后,这个MyThread1又因为人家调用了getThread(runnableN),而被返回,这个时候,对方(按你的设计思路)必须调用start()。但是,由于这个线程已经start过,且已经结束,再start()只会获得:IllegalThreadStateException
你这个实现就有很大的问题了参考下java5.0本身的那个ExecutorService
他的线程池才是"pool"
他的线程池负责维护一批核心线程,这些核心线程用于实现处理任务
你加入的线程对象被线程池托管,有池把需要执行的线程作为任务,交给核心线程,由核心线程调用run函数来执行动作