网站首页 语言 会计 电脑 医学 资格证 职场 文艺体育 范文
当前位置:书香门第 > 计算机 > java语言

java中通用的线程池实例代码

栏目: java语言 / 发布于: / 人气:1.96W

  复制代码 代码如下:

java中通用的线程池实例代码

package Task;

import ection;

import or;

/**

* 任务分发器

*/

public class TaskManage extends Thread

{

protected Vectortasks = new 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(Collectiontasks)

{

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 Vectorthreads = new 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 (Iteratoritr = ator(); ext();)

{

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(Collectiontasks)

{

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() + "..................");

}

}