java中通用的線程池實例代碼

來源:文萃谷 1.81W

  複製代碼 代碼如下:

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

}

}

熱門標籤