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

更新时间:2019-02-22 来源:热点资讯 点击:

【www.0477edu.com--热点资讯】

  复制代码 代码如下:

  package com.smart.frame.task.autoTask;

  import java.util.Collection;

  import java.util.Vector;

  /**

  * 任务分发器

  */

  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)

  {

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

  {

  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 Vectorthreads = new Vector();

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

  {

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

  }

  }

本文来源:http://www.0477edu.com/thread-44955-1-1.html

推荐内容

为您推荐

[智学网登陆首页]2017年智学网登陆及使用流程

导语:关于智学网大家都知道是什么吗?作为一名教师都知道要怎么用了吗?如果还不清楚,我们就一起来看看下面的使用流程吧。智学网教师端网页登录平台是一款智学网登陆查分首页(家长登录网站),智阅卷查成绩,用户可以在这里给学生布置作业,对作业和试卷进行批改,还可以一键快速查看优秀的成绩,让学生们第一时间了解自

2019-04-08 01:37:00   智学网登录查成绩2017  

感冒英语怎么说啊_关于感冒的英语短语

引言:学英语的过程中我们会接触到各种关于疾病的词汇,下面小编来跟大家分享一下若是患上感冒,我们该如何表达症状及治疗方法。近几天,小编重感冒(catch a bad cold),感觉非常难受。第一天,感觉嗓子疼(have a sore throat),出现感冒症状(symptom)。第二天,感冒症状全

2019-02-05 04:06:50   感冒说说短语  

交通规则用英语怎么说|关于交通规则的100个英语短语

无规矩不成方圆,无论我们是身处在国内还是国外都应该遵守交通规则。所以下面我们就来看看关于交通规则的短语吧!1 交通规则 traffic regulation2 路标 guide post3 里程碑 milestone4 停车标志 mark car stop5 红绿灯 traffic lig

2019-02-04 20:17:27   100个英语短语造句  

[考研英语单词app]考研英语单词复习该用什么书

引言:同学们正努力为考研备战之余小编特地来给大家一点福利,推荐几本可用于考研英语单词复习的优秀参考书,希望各位小伙伴喜欢。(1)新东方乱序版本书完整收录大纲词汇,按难度区分核心单词和简单单词,补充超纲单词。精选单词释义,标注常考释义,核心单词“乱序”编排并提供考频。采用&ld

2019-02-03 22:01:00  

【一至十二月的英语单词怎么写】一月到十二月的英语单词怎么写

引言:下面是小编为大家整理的一月到十二月的英语单词的拼写,括号内为缩写。 1月 January (Jan )2月 February (Feb )3 月 March (Mar )4月 April (Apr )5月 May (May )6月 June (Jun )7月 July (Jul )8月 Au

2019-02-03 22:01:00   一至十二月的英语单词