当前位置: 首页 > news >正文

模拟实现线程池(线程数目为定值)和定时器

前言

昨天学习关于定时器的相关知识。今天花时间去模拟实现了一个定时器,同时也去模拟实现了一个线程池(线程数目为定值)。我感觉我收获了很多,对于线程的理解加深了。跟大家分享一下~

 线程池和定时器(这个是主要)的实现

代码 

线程池

import java.util.concurrent.ArrayBlockingQueue;
import java.util.concurrent.BlockingQueue;
class MyThreadPool{//.创建任务队列private BlockingQueue<Runnable> blockingQueue=null;//.创建固定线程数目的线程池public MyThreadPool(int n) throws InterruptedException {//给任务队列给出容量大小blockingQueue=new ArrayBlockingQueue<>(1000);//创建线程for(int i=0;i<n;i++){Thread t=new Thread(()->{Runnable task= null;try {while(true){task = blockingQueue.take();task.run();}} catch (InterruptedException e) {throw new RuntimeException(e);}});
//          t.setDaemon(true);t.start();}}//提交任务,将任务放入任务队列public void submit(Runnable task) throws InterruptedException {blockingQueue.put(task);}
}public class demo40 {public static void main(String[] args) throws InterruptedException {MyThreadPool myThreadPool=new MyThreadPool(10);//往线程池里面提交任务for(int i=0;i<100;i++){int id=i;myThreadPool.submit(()->{System.out.println(Thread.currentThread().getName()+" "+id);});}}
}

 定时器

package Maybe;import java.util.PriorityQueue;class Mytimertask implements Comparable<Mytimertask>{private long time;private Runnable task;public Mytimertask(Runnable task,long time){this.task=task;this.time=time;}public void run(){task.run();}public long gettime(){return time;}@Overridepublic int compareTo(Mytimertask o) {return (int)(o.time-this.time);}
}
class Mytimer{Object locker=new Object();PriorityQueue<Mytimertask> queue=new PriorityQueue<>();public void schedule(Runnable task,long delay){synchronized(locker){Mytimertask task1=new Mytimertask(task,System.currentTimeMillis()+delay);queue.offer(task1);locker.notifyAll();}}public Mytimer(){Thread t=new Thread(()->{synchronized(locker){while(true){while(queue.isEmpty()){try {locker.wait();} catch (InterruptedException e) {throw new RuntimeException(e);}}Mytimertask task=queue.peek();if(task.gettime()>System.currentTimeMillis()){try {locker.wait(task.gettime()-System.currentTimeMillis());} catch (InterruptedException e) {throw new RuntimeException(e);}}else{task.run();queue.poll();}}}});t.start();}
}public class may {public static void main(String[] args) {Mytimer mytimer=new Mytimer();mytimer.schedule(new Runnable() {@Overridepublic void run() {System.out.println("hello 3000");}},3000);mytimer.schedule(new Runnable() {@Overridepublic void run() {System.out.println("hello 2000");}},2000);}
}

结语 

本次的分享就到此为止了~ 最近要期末了,自学的东西要慢下来了,等期末过后,再猛猛地学。

希望期末周能快点过~~  

 

 

http://www.xdnf.cn/news/760105.html

相关文章:

  • 告别手动绘图!基于AI的Smart Mermaid自动可视化图表工具搭建与使用指南
  • 【python深度学习】Day 42 Grad-CAM与Hook函数
  • [学习] PID算法原理与实践(代码示例)
  • 高速串行接口
  • ESG体系
  • Cursor 中三个选项 Agent 、 Ask 和 Manual 含义
  • Python打卡 DAY 42
  • 6、修改和校正时间
  • 设计心得——抽象
  • Leetcode 3566. Partition Array into Two Equal Product Subsets
  • Go 语言中的 panic 详解
  • 【模拟电子电路-工具使用】
  • C++四种类型转换方式
  • 【DAY36】复习日
  • python学习打卡day42
  • python爬虫:RoboBrowser 的详细使用
  • 【目标检测】检测网络中neck的核心作用
  • c++ 类型转换函数
  • Relational Algebra(数据库关系代数)
  • ps中通过拷贝的图层和通过剪切的图层
  • Scratch节日 | 六一儿童节抓糖果
  • Qt|实现将QTreeWidget类item对象鼠标左键拖拽效果到QWidget接收
  • PCIe Gen6相比Gen5的新特性、优化点
  • c++之数组
  • latex 三线表-算法对比表
  • 理解解释器架构:原理、组成与运行机制全解析
  • leetcode hot100 二叉树(一)
  • c++之分支
  • SOC-ESP32S3部分:24-WiFi配网
  • 【PyQt5】从零开始的PyQt5 - QLabel篇