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

银川网站建站百度竞价推广怎么做效果好

银川网站建站,百度竞价推广怎么做效果好,谁用fun域名做网站了,单双免费网站建设文章目录 什么是定时器标准库中的定时器实现定时器 什么是定时器 定时器是一种编程工具,用于特定时间或固定时间间隔执行任务(代码逻辑),其核心功能是管理任务的调度和执行 标准库中的定时器 Timer 和 TimerTask 类 定位&#…

文章目录

    • 什么是定时器
    • 标准库中的定时器
    • 实现定时器

什么是定时器

定时器是一种编程工具,用于特定时间或固定时间间隔执行任务(代码逻辑),其核心功能是管理任务的调度和执行

标准库中的定时器

  1. Timer 和 TimerTask 类
    定位:早期的单线程定时器实现,适用于简单场景。
    核心类:
    Timer:用于调度任务。
    TimerTask:抽象类,需继承并实现 run() 定义任务逻辑。

实现定时器

import java.util.concurrent.PriorityBlockingQueue;
import java.util.concurrent.TimeUnit;class Mytask implements Comparable<Mytask>{private Runnable runnable;// 定义任务执行时间private long time;public Mytask(Runnable runnable,long delay){if(runnable==null){try{throw new IllegalAccessException("任务不能为空");} catch (IllegalAccessException e) {throw new RuntimeException(e);}}if(delay<0){try {throw new IllegalAccessException("时间不能小于0");} catch (IllegalAccessException e) {throw new RuntimeException(e);}}this.runnable=runnable;//计算出任务的具体时间this.time=delay+System.currentTimeMillis();}public Runnable getRunnable() {return runnable;}public long getTime() {return time;}@Overridepublic int compareTo(Mytask o) {if(this.getTime()-o.getTime()>0)return 1;else if(this.getTime()-o.getTime()==0)return 0;else return -1;}
}public class MyTimer {//定义线程内容private PriorityBlockingQueue<Mytask> priorityBlockingQueue=new PriorityBlockingQueue<>();public MyTimer() {//扫描线程Thread th1 = new Thread (()-> {while (true) {try {Mytask take = priorityBlockingQueue.take();//判断有没有到时间long times = System.currentTimeMillis();if (times >= take.getTime()) {//时间到了执行任务take.getRunnable().run();} else {long waittime = take.getTime() - times;//没有到时间放回阻塞队列priorityBlockingQueue.put(take);synchronized (this) {//进行休眠this.wait(waittime);}}} catch (InterruptedException e) {throw new RuntimeException(e);}}});th1.start();//创建一个后台线程Thread thread = new Thread (()->{while (true) {synchronized (this) {notifyAll();}//休眠一会try {TimeUnit.MICROSECONDS.sleep(100);} catch (InterruptedException e) {throw new RuntimeException(e);}}});//设置为后台线程thread.setDaemon(true);thread.start();}public void schedule(Runnable runnable,long time) {//构建一个Mytask对象Mytask mytask=new Mytask(runnable,time);//放入阻塞数组priorityBlockingQueue.put(mytask);//唤醒等待线程synchronized (this){this.notifyAll();}}
}

在这里插入图片描述
在这里插入图片描述
这里要注意当将锁在加在整个while(扫描线程)时会产生死锁

/扫描线程Thread th1 = new Thread (()-> {synchronized (this) {while (true) {try {Mytask take = priorityBlockingQueue.take();//判断有没有到时间long times = System.currentTimeMillis();if (times >= take.getTime()) {//时间到了执行任务take.getRunnable().run();} else {long waittime = take.getTime() - times;//没有到时间放回阻塞队列priorityBlockingQueue.put(take);//进行休眠this.wait(waittime);}} catch (InterruptedException e) {throw new RuntimeException(e);}}}});

在这里插入图片描述
引起死锁的原因

锁的持有与阻塞调用:
扫描线程 th1 的整个循环被包裹在 synchronized(this) 块中。
当队列为空时,priorityBlockingQueue.take() 会阻塞,但此时 th1 仍持有 this 锁。
其他线程(如主线程调用 schedule)需要获取 this 锁才能添加任务,但无法获得锁,导致它们被阻塞。

互相等待的僵局:
th1 在等待队列中有任务(被 take() 阻塞),但其他线程无法添加任务(因为它们需要 this 锁)。
其他线程在等待 th1 释放锁,而 th1 在等待其他线程添加任务,形成死锁。

1.synchronized和wait的使用:
在MyTimer类的构造方法中,扫描线程t1并使synchronized(this)进行同步,台线程 (thread) 也使用 synchronized (this) 来调用 notifyAll()。

2.wait 和 notifyAll 的逻辑
在从队列中拿到线程任务时如果线程任务还没有到会调用wait()进行等待,
在主线程在这里插入图片描述
后台线程 (thread) 也在尝试调用 notifyAll(),但它是在 synchronized (this) 块中执行的。如果此时没有其他线程持有 MyTimer 对象的锁,后台线程将无法执行 notifyAll()。所以锁得不到释放
任务的执行时间:

3.如果 Mytask 的执行时间设置得非常短(例如 0 毫秒),可能会导致 th1 线程不断地将任务放回队列并调用 wait(),而后台线程可能无法及时调用 notifyAll(),从而导致 th1 永远处于等待状态。

http://www.dtcms.com/wzjs/260790.html

相关文章:

  • 宝安区城市建设局网站学网络运营需要多少钱
  • wordpress的页面标题杭州seo顾问
  • 怎么做代刷网网站app宁波seo网络推广外包报价
  • 给客户做非法网站seminar是什么意思
  • 烟台网站排名优化价格适合40岁女人的培训班
  • 百度推广做网站什么价位全媒体运营师培训费用
  • 政府网站建设技术员工资多少抄一则新闻四年级
  • 小米品牌vi设计南平seo
  • 网站建设开发方式包括一l丫网络技术培训
  • 一级a做爰片付费网站网络推广员要怎么做
  • 陕西省建设执业注册中心网站app线下推广怎么做
  • 深圳宝安建网站百度seo优化策略
  • 南京房地产网站建设百度推广代理商与总公司的区别
  • 中国建筑设计咨询公司搜索引擎优化文献
  • 做网站用什么面板好网站外链是什么意思
  • 清镇网站建设神马网站快速排名案例
  • 做网站用小型机或服务器做网络推广一般是什么专业
  • 免费素材网站哪个最好seo关键词排名软件流量词
  • 做直播网站需要哪些技术seo自动点击排名
  • 网站首页被挂黑链企业培训课程安排表
  • 网站建设委托合同网站关键词排名批量查询
  • 做购物网站需要什么资质外包公司和劳务派遣的区别
  • 长沙网络建设的网站网页模板源代码
  • jspajax网站开发典型实例网页制作html代码
  • 做soho 怎么建立网站手机关键词seo排名优化
  • 做网站的职业叫什么深圳推广系统
  • 可以免费注册网站金华seo全网营销
  • 招聘网官方网站口碑营销的优势有哪些
  • wordpress博客福利网整站源码国外网站如何搭建网页
  • 云主机开网站教程渠道营销推广方案