简单的线程池实现

朴灿烈づ我的快乐病毒、 2022-05-24 12:09 270阅读 0赞

为了节省系统在并发时不断创建和销毁线程所带来的额外开销,就需要引入线程池。线程池的基本思想是,线程使用完后并不销毁,而是一直处于等待状态,如果下一个任务进入,可以再使用这个线程执行,这样就减少了线程的创建和销毁。
首先是线程池的实现:

  1. package com.mr.smart.future.simplethreadpool;
  2. import java.util.List;
  3. import java.util.Vector;
  4. public class ThreadPool {
  5. private static ThreadPool instance = null;
  6. //空闲的线程队列
  7. private List<PThread> idleThreads;
  8. //已有的线程总数
  9. private int threadCounter;
  10. private boolean isShutDown = false;
  11. private ThreadPool() {
  12. this.idleThreads = new Vector<>(5);
  13. threadCounter = 0;
  14. }
  15. public int getThreadCounter() {
  16. return threadCounter;
  17. }
  18. //取得线程池的实例
  19. public synchronized static ThreadPool getInstance() {
  20. if (instance == null)
  21. instance = new ThreadPool();
  22. return instance;
  23. }
  24. //将线程放入池中
  25. protected synchronized void repool(PThread repoolingThread) {
  26. if (!isShutDown) {
  27. idleThreads.add(repoolingThread);
  28. } else {
  29. repoolingThread.shutDown();
  30. }
  31. }
  32. //停止池中所有线程
  33. public synchronized void shutDown() {
  34. isShutDown = true;
  35. for (int threadIndex = 0; threadIndex < idleThreads.size(); threadIndex++) {
  36. PThread idleThread = idleThreads.get(threadIndex);
  37. idleThread.shutDown();
  38. }
  39. }
  40. //执行任务
  41. public synchronized void start(Runnable target) {
  42. PThread thread = null;
  43. if (idleThreads.size() > 0) {
  44. int lastIndex = idleThreads.size() - 1;
  45. thread = (PThread) idleThreads.get(lastIndex);
  46. idleThreads.remove(lastIndex);
  47. //立即执行这个任务
  48. thread.setTarget(target);
  49. } else {
  50. threadCounter++;
  51. thread = new PThread(target, "PThread #" + threadCounter, this);
  52. // 启动这个线程
  53. thread.start();
  54. }
  55. }
  56. }

配合线程:

  1. package com.mr.smart.future.simplethreadpool;
  2. public class PThread extends Thread {
  3. //线程池
  4. private ThreadPool pool;
  5. //任务
  6. private Runnable target;
  7. private boolean isShutDown = false;
  8. private boolean isIdle = false;
  9. //构造函数
  10. public PThread(Runnable target, String name, ThreadPool pool) {
  11. super(name);
  12. this.pool = pool;
  13. this.target = target;
  14. }
  15. public Runnable getTarget() {
  16. return target;
  17. }
  18. public boolean isIdle() {
  19. return isIdle;
  20. }
  21. @Override
  22. public void run() {
  23. //只要没有关闭,则一直不结束该线程
  24. while (!isShutDown) {
  25. isIdle = false;
  26. if (target != null) {
  27. //运行任务
  28. target.run();
  29. }
  30. //任务结束了,到闲置状态
  31. isIdle = true;
  32. try {
  33. //该任务结束后,不关闭线程,而是放入线程池空闲队列
  34. pool.repool(this);
  35. synchronized (this) {
  36. //线程空闲,等待新的任务到来
  37. wait();
  38. }
  39. } catch (InterruptedException e) {
  40. }
  41. isIdle = false;
  42. }
  43. }
  44. public synchronized void setTarget(Runnable newTarget) {
  45. target = newTarget;
  46. notifyAll();
  47. }
  48. //关闭线程
  49. public synchronized void shutDown() {
  50. isShutDown = true;
  51. notifyAll();
  52. }
  53. }

发表评论

表情:
评论列表 (有 0 条评论,270人围观)

还没有评论,来说两句吧...

相关阅读

    相关 c++实现简单线

        线程池,先创建一定数目的线程,初始都处于空闲状态。当有新的任务进来,从线程池中取出一个空闲的线程处理任务,处理完成之后,该线程被重新放回到线程池中。当线程池中的线程都在

    相关 线介绍及简单实现

    2002 年 8 月 22 日 > 服务器程序利用线程技术响应客户请求已经司空见惯,可能您认为这样做效率已经很高,但您有没有想过优化一下使用线程的方法。该文章将向您介绍服务器

    相关 【Linux】 实现一个简单线

    线程池的概念 “池”的概念大家应该不陌生,线程池顾名思义就是存放线程的“池子”。 它是一种线程的使用模式。我们发现如果线程过多会带来调度的开销,进而影响缓存的局部性和