java中通用的线程池实例代码
复制代码 代码如下:
package Task;
import ection;
import or;
/**
* 任务分发器
*/
public class TaskManage extends Thread
{
protected Vector
protected boolean running = false;
protected boolean stopped = false;
protected boolean paused = false;
protected boolean killed = false;
private ThreadPool pool;
public TaskManage(ThreadPool pool)
{
= pool;
}
public void putTask(Runnable task)
{
(task);
}
public void putTasks(Runnable[] tasks)
{
for (int i = 0; i < th; i++)
(tasks[i]);
}
public void putTasks(Collection
{
ll(tasks);
}
protected Runnable popTask()
{
if (() > 0) return (Runnable) ve(0);
else return null;
}
public boolean isRunning()
{
return running;
}
public void stopTasks()
{
stopped = true;
}
public void stopTasksSync()
{
stopTasks();
while (isRunning())
{
try
{
sleep(5);
}
catch (InterruptedException e)
{
esultMessage(e);
}
}
}
public void pauseTasks()
{
paused = true;
}
public void pauseTasksSync()
{
pauseTasks();
while (isRunning())
{
try
{
sleep(5);
}
catch (InterruptedException e)
{
esultMessage(e);
}
}
}
public void kill()
{
if (!running) interrupt();
else killed = true;
}
public void killSync()
{
kill();
while (isAlive())
{
try
{
sleep(5);
}
catch (InterruptedException e)
{
esultMessage(e);
}
}
}
public synchronized void startTasks()
{
running = true;
fy();
}
public synchronized void run()
{
try
{
while (true)
{
if (!running || () == 0)
{
fyForIdleThread();
();
}
else
{
Runnable task;
while ((task = popTask()) != null)
{
();
if (stopped)
{
stopped = false;
if (() > 0)
{
r();
tln(entThread()d() + ": Tasks are stopped");
break;
}
}
if (paused)
{
paused = false;
if (() > 0)
{
tln(entThread()d() + ": Tasks are paused");
break;
}
}
}
running = false;
}
if (killed)
{
killed = false;
break;
}
}
}
catch (InterruptedException e)
{
esultMessage(e);
return;
}
}
}
复制代码 代码如下:
package Task;
import ection;
import ator;
import or;
/**
* 线程池
*/
public class ThreadPool
{
protected int maxPoolSize = oolSize;
protected int initPoolSize = PoolSize;
protected Vector
protected boolean initialized = false;
protected boolean hasIdleThread = false;
public ThreadPool()
{
super();
}
public ThreadPool(int maxPoolSize, int initPoolSize)
{
oolSize = maxPoolSize;
PoolSize = initPoolSize;
}
public void init()
{
initialized = true;
for (int i = 0; i < initPoolSize; i++)
{
TaskManage thread = new TaskManage(this);
t();
(thread);
}
}
public void setMaxPoolSize(int maxPoolSize)
{
oolSize = maxPoolSize;
if (maxPoolSize < getPoolSize()) setPoolSize(maxPoolSize);
}
/**
* 重设当前线程数 若需杀掉某线程,线程不会立刻杀掉,而会等到线程中的事
* 务处理完成 但此方法会立刻从线程池中移除该线程,不会等待事务处理结束
*/
public void setPoolSize(int size)
{
if (!initialized)
{
initPoolSize = size;
return;
}
else if (size > getPoolSize())
{
for (int i = getPoolSize(); i < size && i < maxPoolSize; i++)
{
TaskManage thread = new TaskManage(this);
t();
(thread);
}
}
else if (size < getPoolSize())
{
while (getPoolSize() > size)
{
TaskManage th = (TaskManage) ve(0);
();
}
}
}
public int getPoolSize()
{
return ();
}
protected void notifyForIdleThread()
{
hasIdleThread = true;
}
protected boolean waitForIdleThread()
{
hasIdleThread = false;
while (!hasIdleThread && getPoolSize() >= maxPoolSize)
{
try
{
p(5);
}
catch (InterruptedException e)
{
esultMessage(e);
return false;
}
}
return true;
}
public synchronized TaskManage getIdleThread()
{
while (true)
{
for (Iterator
{
TaskManage th = (TaskManage) ();
if (!nning()) return th;
}
if (getPoolSize() < maxPoolSize)
{
TaskManage thread = new TaskManage(this);
t();
(thread);
return thread;
}
if (waitForIdleThread() == false) return null;
}
}
public void processTask(Runnable task)
{
TaskManage th = getIdleThread();
if (th != null)
{
ask(task);
tTasks();
}
}
public void processTasksInSingleThread(Runnable[] tasks)
{
TaskManage th = getIdleThread();
if (th != null)
{
asks(tasks);
tTasks();
}
}
public void processTasksInSingleThread(Collection
{
TaskManage th = getIdleThread();
if (th != null)
{
asks(tasks);
tTasks();
}
}
}
复制代码 代码如下:
package Task;
public class TopTask implements Runnable
{
private ThreadPool pool;
public TopTask()
{
super();
}
public TopTask(ThreadPool pool)
{
super();
= pool;
}
@Override
public void run()
{
init();
start();
}
/**
* 初始化验证权限、参数之类
*/
public void init()
{
}
/**
* 开始自动任务
*/
public void start()
{
for (int i = 0; i < 10; i++)
{
essTask(new BeginAuto());
}
}
}
/**
* 实现类
*/
class BeginAuto implements Runnable
{
@Override
public void run()
{
tln(entThread()d() + "..................");
}
}
-
java的应用范围
Java是一种可以撰写跨平台应用软件的面向对象的程序设计语言。自从sun公司推出java语言以来,java极大受到开发者和企业的欢迎,在互联网如此发达的今天,java语言得到了广泛的应用,比如网站,游戏,电子商务等,java语言在这些领域开发占很大的比例。在全球云计算和移动互...
-
Java语法基础for语句练习
本文是本站小编搜索整理的关于Java语法基础for语句练习,供参考学习,希望对大家有所帮助!想了解更多相关信息请持续关注我们应届毕业生考试网!控制语句——for练习语句的嵌套应用累加求和,计数器循环嵌套一、语句的嵌套应用语句嵌套形式。其实就是语句中...
-
java环境变量设置path
导语:java的环境变量是怎么设置的呢?以下小编为大家介绍java环境变量设置path文章,仅供参考!java环境变量设置path环境变量是一个具有特定名字的对象,它包含了一个或者多个应用程序所将使用到的信息。例如path,当要求系统运行一个程序而没有告诉它程序所在的完整路...
-
java设计模式:工厂模式
导语:Java是一门面向对象编程语言,不仅吸收了C++语言的各种优点,还摒弃了C++里难以理解的多继承、指针等概念,因此Java语言具有功能强大和简单易用两个特征。下面我们来看看工厂模式,希望对大家有所帮助。工厂设计模式★场景和问题面向接口编程是java编程中的大家所...