[Java] 简单的线程池与任务队列 →→→→→进入此内容的聊天室

来自 , 2019-11-26, 写在 Java, 查看 175 次.
URL http://www.code666.cn/view/a7608800
  1. import java.util.*;
  2.  
  3. /**
  4.  * 简单的线程池与任务队列
  5.  *
  6.  */
  7. public class WorkQueue {
  8.         private final int nThreads;// 线程池的大小
  9.         private final PoolWorker[] threads;// 用数组实现线程池
  10.         private final LinkedList queue;// 任务队列
  11.  
  12.         public WorkQueue(int nThreads) {
  13.                 this.nThreads = nThreads;
  14.                 queue = new LinkedList();
  15.                 threads = new PoolWorker[nThreads];
  16.  
  17.                 for (int i = 0; i < nThreads; i++) {
  18.                         threads[i] = new PoolWorker();
  19.                         threads[i].start();// 启动所有工作线程
  20.                 }
  21.         }
  22.  
  23.         public void execute(Runnable r) {// 执行任务
  24.                 synchronized (queue) {
  25.                         queue.addLast(r);
  26.                         queue.notify();
  27.                 }
  28.         }
  29.  
  30.         private class PoolWorker extends Thread {// 工作线程类
  31.                 public void run() {
  32.                         Runnable r;
  33.                         while (true) {
  34.                                 synchronized (queue) {
  35.                                         while (queue.isEmpty()) {// 如果任务队列中没有任务,等待
  36.                                                 try {
  37.                                                         queue.wait();
  38.                                                 } catch (InterruptedException ignored) {
  39.                                                 }
  40.                                         }
  41.                                         r = (Runnable) queue.removeFirst();// 有任务时,取出任务
  42.                                 }
  43.                                 try {
  44.                                         r.run();// 执行任务
  45.                                 } catch (Exception e) {
  46.                                         // TODO: handle exception
  47.                                 }
  48.                         }
  49.                 }
  50.         }
  51.  
  52.         public static void main(String args[]) {
  53.                 WorkQueue wq = new WorkQueue(10);// 10个工作线程
  54.                 Mytask r[] = new Mytask[20];// 20个任务
  55.  
  56.                 for (int i = 0; i < 20; i++) {
  57.                         r[i] = new Mytask();
  58.                         wq.execute(r[i]);
  59.                 }
  60.         }
  61. }
  62.  
  63. class Mytask implements Runnable {// 任务接口
  64.         public void run() {
  65.                 String name = Thread.currentThread().getName();
  66.                 try {
  67.                         Thread.sleep(100);// 模拟任务执行的时间
  68.                 } catch (Exception e) {
  69.                         // TODO: handle exception
  70.                 }
  71.  
  72.                 System.out.println(name + " executed OK");
  73.         }
  74. }

回复 "简单的线程池与任务队列"

这儿你可以回复上面这条便签

captcha