java中通用的線程池執行個體代碼

來源:互聯網
上載者:User

複製代碼 代碼如下:package com.smart.frame.task.autoTask;

import java.util.Collection;
import java.util.Vector;

/**
* 任務分發器
*/
public class TaskManage extends Thread
{
protected Vector<Runnable> tasks = new Vector<Runnable>();
protected boolean running = false;
protected boolean stopped = false;
protected boolean paused = false;
protected boolean killed = false;
private ThreadPool pool;

public TaskManage(ThreadPool pool)
{
this.pool = pool;
}

public void putTask(Runnable task)
{
tasks.add(task);
}

public void putTasks(Runnable[] tasks)
{
for (int i = 0; i < tasks.length; i++)
this.tasks.add(tasks[i]);
}

public void putTasks(Collection<Runnable> tasks)
{
this.tasks.addAll(tasks);
}

protected Runnable popTask()
{
if (tasks.size() > 0) return (Runnable) tasks.remove(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)
{
TaskException.getResultMessage(e);
}
}
}

public void pauseTasks()
{
paused = true;
}

public void pauseTasksSync()
{
pauseTasks();
while (isRunning())
{
try
{
sleep(5);
}
catch (InterruptedException e)
{
TaskException.getResultMessage(e);
}
}
}

public void kill()
{
if (!running) interrupt();
else killed = true;
}

public void killSync()
{
kill();
while (isAlive())
{
try
{
sleep(5);
}
catch (InterruptedException e)
{
TaskException.getResultMessage(e);
}
}
}

public synchronized void startTasks()
{
running = true;
this.notify();
}

public synchronized void run()
{
try
{
while (true)
{
if (!running || tasks.size() == 0)
{
pool.notifyForIdleThread();
this.wait();
}
else
{
Runnable task;
while ((task = popTask()) != null)
{
task.run();
if (stopped)
{
stopped = false;
if (tasks.size() > 0)
{
tasks.clear();
System.out.println(Thread.currentThread().getId() + ": Tasks are stopped");
break;
}
}
if (paused)
{
paused = false;
if (tasks.size() > 0)
{
System.out.println(Thread.currentThread().getId() + ": Tasks are paused");
break;
}
}
}
running = false;
}

if (killed)
{
killed = false;
break;
}
}
}
catch (InterruptedException e)
{
TaskException.getResultMessage(e);
return;
}
}
}

複製代碼 代碼如下:package com.smart.frame.task.autoTask;

import java.util.Collection;
import java.util.Iterator;
import java.util.Vector;

/**
* 線程池
*/
public class ThreadPool
{
protected int maxPoolSize = TaskConfig.maxPoolSize;
protected int initPoolSize = TaskConfig.initPoolSize;
protected Vector<TaskManage> threads = new Vector<TaskManage>();
protected boolean initialized = false;
protected boolean hasIdleThread = false;

public ThreadPool()
{
super();
}

public ThreadPool(int maxPoolSize, int initPoolSize)
{
this.maxPoolSize = maxPoolSize;
this.initPoolSize = initPoolSize;
}

public void init()
{
initialized = true;
for (int i = 0; i < initPoolSize; i++)
{
TaskManage thread = new TaskManage(this);
thread.start();
threads.add(thread);
}
}

public void setMaxPoolSize(int maxPoolSize)
{
this.maxPoolSize = 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);
thread.start();
threads.add(thread);
}
}
else if (size < getPoolSize())
{
while (getPoolSize() > size)
{
TaskManage th = (TaskManage) threads.remove(0);
th.kill();
}
}
}

public int getPoolSize()
{
return threads.size();
}

protected void notifyForIdleThread()
{
hasIdleThread = true;
}

protected boolean waitForIdleThread()
{
hasIdleThread = false;
while (!hasIdleThread && getPoolSize() >= maxPoolSize)
{
try
{
Thread.sleep(5);
}
catch (InterruptedException e)
{
TaskException.getResultMessage(e);
return false;
}
}

return true;
}

public synchronized TaskManage getIdleThread()
{
while (true)
{
for (Iterator<TaskManage> itr = threads.iterator(); itr.hasNext();)
{
TaskManage th = (TaskManage) itr.next();
if (!th.isRunning()) return th;
}

if (getPoolSize() < maxPoolSize)
{
TaskManage thread = new TaskManage(this);
thread.start();
threads.add(thread);
return thread;
}

if (waitForIdleThread() == false) return null;
}
}

public void processTask(Runnable task)
{
TaskManage th = getIdleThread();
if (th != null)
{
th.putTask(task);
th.startTasks();
}
}

public void processTasksInSingleThread(Runnable[] tasks)
{
TaskManage th = getIdleThread();
if (th != null)
{
th.putTasks(tasks);
th.startTasks();
}
}

public void processTasksInSingleThread(Collection<Runnable> tasks)
{
TaskManage th = getIdleThread();
if (th != null)
{
th.putTasks(tasks);
th.startTasks();
}
}

}

複製代碼 代碼如下:package com.smart.frame.task.autoTask;

public class TopTask implements Runnable
{

private ThreadPool pool;

public TopTask()
{
super();
}

public TopTask(ThreadPool pool)
{
super();
this.pool = pool;
}

@Override
public void run()
{
init();
start();
}

/**
* 初始化驗證許可權、參數之類
*/
public void init()
{

}

/**
* 開始自動任務
*/
public void start()
{
for (int i = 0; i < 10; i++)
{
pool.processTask(new BeginAuto());
}
}
}
/**
* 實作類別
*/
class BeginAuto implements Runnable
{
@Override
public void run()
{
System.out.println(Thread.currentThread().getId() + "..................");
}

}

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.