Java实现的 线程池
由于最近开始学习java,用到了线程池,按照之前c++的写法写出此java版的线程池
TaskRunnale实现相关任务的接口,具体要实现什么任务在相应的run函数中实现。
package threadpool; import java.util.ArrayDeque;
import java.util.ArrayList;
import java.util.List;
import java.util.Queue; class TaskRunnale implements Runnable
{
public void run()
{
System.out.println(this.hashCode() + "Testrunner in Works");
//此方法使本线程睡眠5秒
synchronized (this)
{
try
{
wait(1000);//等待5秒时间
}
catch (InterruptedException ioe)
{
}
}
System.out.println(this.hashCode() + "Testrunner out Works");
}
} class Thread_InPool extends Thread
{
private ThreadPool pool_ = null; // 池中线程需要知道自己所在的池
private Runnable target_ = null; // 线程的任务
private boolean shutDown_ = false;
private boolean idle_ = false;//设置是否让线程处于等待状态 public Thread_InPool(ThreadPool spool)
{ super();
pool_ = spool;
start();
} public void run()
{
// 这个循环不能结束,除非池类要求线程结束
// 每一次循环都会执行一次池类分配给的任务target
while (!shutDown_)
{ idle_ = false;
if (target_ != null)
{
target_.run(); // 运行target中的代码
target_ = null; if(pool_.SwitchThread(this) == 1)
continue; }
else
{
idle_ = true;
synchronized (this)
{
try
{
wait(2000);//等待2秒时间
}
catch (InterruptedException ioe)
{
}
}
} }
//循环这里不能结束,否则线程结束,资源被VM收回,
//就无法起到线程池的作用了
} public synchronized void setTarget(java.lang.Runnable newTarget)
{//设置新的target,并唤醒睡眠中的线程
target_ = newTarget; // 新任务
notifyAll(); // 唤醒睡眠的线程
} public synchronized void shutDown()
{
shutDown_ = true;
notifyAll();
}
} public class ThreadPool { private static ThreadPool instance_ = null;
public static final int LOW_PRIORITY = 0;
public static final int NORMAL_PRIORITY = 1;
public static final int HIGH_PRIORITY = 2; private static int m_Threadcount = 4;
private List<Thread_InPool> m_IdleThread = new ArrayList<Thread_InPool>(); //空闲的线程
private List<Thread_InPool> m_ActiveThread = new ArrayList<Thread_InPool>();//处于任务执行的线程
private Queue<Runnable> m_RunableTarger = new ArrayDeque<Runnable>(); ;//任务队列,此处用Queue,是考虑到可以有任务优先级的扩展 private ThreadPool(int initcount)
{
m_Threadcount = initcount > 0?initcount:4;
for(int i = 0 ;i< m_Threadcount ; i++)
{
m_IdleThread.add(new Thread_InPool(this));
}
} public static ThreadPool Get_Instance() {
if (instance_ == null)
instance_ = new ThreadPool(3);
return instance_;
} public synchronized void DestroyPool()
{
for(int i = 0 ;i< m_IdleThread.size();i++)
{ Thread_InPool dthread = m_IdleThread.get(i);
dthread.shutDown();
}
for(int i = 0 ;i< m_ActiveThread.size();i++)
{ Thread_InPool dthread = m_ActiveThread.get(i);
dthread.shutDown();
} } public synchronized void InsertTarget(java.lang.Runnable newTarget) //插入新的任务
{
if(newTarget == null) return; if(!m_IdleThread.isEmpty())
{
Thread_InPool i_thread = null;
i_thread = m_IdleThread.get(m_IdleThread.size()-1);
m_IdleThread.remove(i_thread);
i_thread.setTarget(newTarget);
m_ActiveThread.add(i_thread);
}
else
{
m_RunableTarger.add(newTarget);
}
} public synchronized int SwitchThread(Thread_InPool switchThread) //任务结束后,如果任务队列不为空,则继续执行新的任务,否则入空队列
{
int rvalue = 0;
if(!m_RunableTarger.isEmpty())
{
java.lang.Runnable o_Target = m_RunableTarger.poll();
if(o_Target != null)
{
switchThread.setTarget(o_Target);
rvalue = 1;
}
}
else
{
m_IdleThread.add(switchThread);
m_ActiveThread.remove(switchThread);
}
return rvalue;
}
}
如果代码存在什么问题,欢迎指正,相关学习
Java实现的 线程池的更多相关文章
- Java 四种线程池newCachedThreadPool,newFixedThreadPool,newScheduledThreadPool,newSingleThreadExecutor
介绍new Thread的弊端及Java四种线程池的使用,对Android同样适用.本文是基础篇,后面会分享下线程池一些高级功能. 1.new Thread的弊端执行一个异步任务你还只是如下new T ...
- Java四种线程池
Java四种线程池newCachedThreadPool,newFixedThreadPool,newScheduledThreadPool,newSingleThreadExecutor 时间:20 ...
- java笔记--使用线程池优化多线程编程
使用线程池优化多线程编程 认识线程池 在Java中,所有的对象都是需要通过new操作符来创建的,如果创建大量短生命周期的对象,将会使得整个程序的性能非常的低下.这种时候就需要用到了池的技术,比如数据库 ...
- Java多线程和线程池
转自:http://blog.csdn.net/u013142781/article/details/51387749 1.为什么要使用线程池 在Java中,如果每个请求到达就创建一个新线程,开销是相 ...
- Java多线程之线程池详解
前言 在认识线程池之前,我们需要使用线程就去创建一个线程,但是我们会发现有一个问题: 如果并发的线程数量很多,并且每个线程都是执行一个时间很短的任务就结束了,这样频繁创建线程就会大大降低系统的效率,因 ...
- Java四种线程池的学习与总结
在Java开发中,有时遇到多线程的开发时,直接使用Thread操作,对程序的性能和维护上都是一个问题,使用Java提供的线程池来操作可以很好的解决问题. 一.new Thread的弊端 执行一个异步任 ...
- java多线程、线程池及Spring配置线程池详解
1.java中为什么要使用多线程使用多线程,可以把一些大任务分解成多个小任务来执行,多个小任务之间互不影像,同时进行,这样,充分利用了cpu资源.2.java中简单的实现多线程的方式 继承Thread ...
- Java高并发 -- 线程池
Java高并发 -- 线程池 主要是学习慕课网实战视频<Java并发编程入门与高并发面试>的笔记 在使用线程池后,创建线程变成了从线程池里获得空闲线程,关闭线程变成了将线程归坏给线程池. ...
- (转载)new Thread的弊端及Java四种线程池的使用
介绍new Thread的弊端及Java四种线程池的使用,对Android同样适用.本文是基础篇,后面会分享下线程池一些高级功能. 1.new Thread的弊端 执行一个异步任务你还只是如下new ...
随机推荐
- poj2385 简单DP
J - 简单dp Crawling in process... Crawling failed Time Limit:1000MS Memory Limit:65536KB 64bit ...
- Codeforces Round #302 (Div. 1)
转载请注明出处: http://www.cnblogs.com/fraud/ ——by fraud A. Writing Code Programmers working on a ...
- 用U盘烧写Uboot.bin到Nor Flash
1.在开发板上面插上U盘 2.打到 NAND flash模式启动U-boot,输入以下命令打开usb设备. [u-boot@SMDK2440A]# usb reset //以重启的方式 或者用 [u- ...
- open_basedir restriction in effect. File() is not within the allowed path(s)
目前发现eaccelerator安装之后如果php.ini中设置open_basedir将导致open_basedir的一些报错(open_basedir restriction in effect. ...
- GSoap的使用(调用webservice接口)
由于本人写项目时使用到C++要调用C#写得后台服务器发布的webservice,因此抽出来了一点时间整理相关的知识如下 gSOAP是一个绑定SOAP/XML到C/C++语言的工具,使用它可以简单快速地 ...
- hdu 1142 A Walk Through the Forest
http://acm.hdu.edu.cn/showproblem.php?pid=1142 这道题是spfa求最短路,然后dfs()求路径数. #include <cstdio> #in ...
- 使用片上XRAM需要进行的初始化
现在,流行的51单片机大多把on-chip expanded RAM(以下简称XRAM)作为基本配置,容量有些差别.厂商在给出芯片特性时,往往把XRAM和标准52芯片的256字节内部RAM加在一起统称 ...
- Delphi 实现任务栏多窗口图标显示(使用WS_EX_APPWINDOW风格)
uses Windows;type TfrmLogin = class(TForm) end; implementation {$R *.dfm} procedure TfrmLogin.FormCr ...
- 关于php-fpm通讯时没有REQUEST_METHOD的问题
nginx是通过fastcgi协议来和php通讯的!而php-fpm就扮演了这样的角色 fastcgi协议 中文版http://blog.chinaunix.net/uid-380521-id-241 ...
- CentOS 6.5配置nfs服务
CentOS 6.5配置nfs服务 网络文件系统(Network File System,NFS),一种使用于分散式文件系统的协议,由升阳公司开发,于1984年向外公布.功能是通过网络让不同的机器.不 ...