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

企业的网站建设费用重庆seo什么意思

企业的网站建设费用,重庆seo什么意思,网站开发需求图,合肥seo公司专栏:JavaEE初阶起飞计划 个人主页:手握风云 目录 一、多线程案例 1.1. 定时器 一、多线程案例 1.1. 定时器 定时器是软件开发的一个重要组件,是一种能够按照预设的时间间隔或在特定时间点执行某个任务或代码片段的机制。你可以把它想象成…

专栏:JavaEE初阶起飞计划

个人主页:手握风云

目录

一、多线程案例

1.1. 定时器


一、多线程案例

1.1. 定时器

        定时器是软件开发的一个重要组件,是一种能够按照预设的时间间隔或在特定时间点执行某个任务或代码片段的机制。你可以把它想象成一个闹钟,只不过这个“闹钟”不是提醒你去起床,而是提醒计算机去执行某个特定的操作。定时器与阻塞队列一致,也会被单独封装成一个或一组服务器来使用。

  • 标准库中的定时器
import java.util.Timer;
import java.util.TimerTask;public class Demo1 {public static void main(String[] args) {Timer timer = new Timer();timer.schedule(new TimerTask() {@Overridepublic void run() {System.out.println("定时器执行任务 3000");}}, 3000);timer.schedule(new TimerTask() {@Overridepublic void run() {System.out.println("定时器执行任务 2000");}}, 2000);timer.schedule(new TimerTask() {@Overridepublic void run() {System.out.println("定时器执行任务 1000");}}, 1000);System.out.println("程序启动");}
}

  • 定时器的实现

        对于自主实现的定时器,需要指定等待的最大时间,如果等不到,还需要执行其他的操作。

class MyTimer {public MyTimer() {}//向定时器中添加任务public void schedule(Runnable runnable, long delay) {}
}

        这里注意,TimerTask类实现的是Runnable接口,所以我们在schedule方法里面添加的也是Runnable类型的参数。既然要对任务进行组织管理,就得使用合适的数据结构,比如顺序表、栈。但是这些任务不一定是按照时间顺序添加的,并且添加的顺序和执行顺序没太大关系。如果使用顺序表,执行任务时,就需要遍历来找到时间最小的任务,效率太低。这时我们就可以使用堆来解决。

        对于堆所存放的泛型参数,这里不能添加成Runnable,因为堆里面的任务不只是内容,还需要考虑任务的时间。

class MyTimerTask {private Runnable task;// 这个地方为了和当前时间对比,确认任务是否执行,需要保存绝对的时间戳private long time;public MyTimerTask(Runnable task, long delay) {this.task = task;this.time = System.currentTimeMillis() + delay;}public Runnable getTask() {return task;}public long getTime() {return time;}
}class MyTimer {private PriorityQueue<MyTimerTask> queue = new PriorityQueue<>();public MyTimer() {}//向定时器中添加任务public void schedule(Runnable runnable, long delay) {queue.offer(new MyTimerTask(runnable, delay));}
}

        由于MyTimerTask是放在优先级队列中,所以我们还需要写出比较规则。

class MyTimerTask implements Comparable<MyTimerTask>{private Runnable task;// 这个地方为了和当前时间对比,确认任务是否执行,需要保存绝对的时间戳private long time;public MyTimerTask(Runnable task, long delay) {this.task = task;this.time = System.currentTimeMillis() + delay;}public Runnable getTask() {return task;}public long getTime() {return time;}@Overridepublic int compareTo(MyTimerTask o) {return (int) (this.time - o.time);}
}

        接下来就是创建线程,让线程来检测任务是否到时间了,以及去执行这个任务。我们需要循环从队列中取出元素,判断是否到时间了,如果到达就出队列,没有就不做处理。

import java.util.PriorityQueue;class MyTimerTask implements Comparable<MyTimerTask>{private Runnable task;// 这个地方为了和当前时间对比,确认任务是否执行,需要保存绝对的时间戳private long time;public MyTimerTask(Runnable task, long delay) {this.task = task;this.time = System.currentTimeMillis() + delay;}public Runnable getTask() {return task;}public long getTime() {return time;}@Overridepublic int compareTo(MyTimerTask o) {return (int) (this.time - o.time);}
}class MyTimer {private PriorityQueue<MyTimerTask> queue = new PriorityQueue<>();public MyTimer() {Thread t = new Thread(() -> {while (true) {if (queue.isEmpty()) {continue;}MyTimerTask task = queue.peek();long curTime = System.currentTimeMillis();if (curTime < task.getTime()) {continue;} else {task.getTask().run();queue.poll();}}});t.start();}//向定时器中添加任务public void schedule(Runnable runnable, long delay) {queue.offer(new MyTimerTask(runnable, delay));}
}public class Demo2 {public static void main(String[] args) {MyTimer timer = new MyTimer();timer.schedule(new Runnable() {@Overridepublic void run() {System.out.println("定时任务:3000");}}, 3000);timer.schedule(new Runnable() {@Overridepublic void run() {System.out.println("定时任务:2000");}}, 2000);timer.schedule(new Runnable() {@Overridepublic void run() {System.out.println("定时任务:1000");}}, 1000);}
}

        虽然执行效果没有什么问题,但上面的操作对于同一个队列进行出入,所以线程是不安全的。那我们就需要在入队列和出队列的操作里面都要进行加锁。第二个问题,就是忙等出现饿死。如上面的代码,当队列为空或者没到时间时,不做任何处理,只消耗CPU资源,没有任何实质性的进展。尤其是第二个continue,这里就相当于30分钟之后要去执行某项任务,每隔1分钟就得看一下时间,当我们设计了等待时间之后,到时间自动唤醒或者有优先级更高的任务要去执行。

        完整代码:

import java.util.PriorityQueue;class MyTimerTask implements Comparable<MyTimerTask>{private Runnable task;// 这个地方为了和当前时间对比,确认任务是否执行,需要保存绝对的时间戳private long time;public MyTimerTask(Runnable task, long delay) {this.task = task;this.time = System.currentTimeMillis() + delay;}public Runnable getTask() {return task;}public long getTime() {return time;}@Overridepublic int compareTo(MyTimerTask o) {return (int) (this.time - o.time);}
}class MyTimer {private PriorityQueue<MyTimerTask> queue = new PriorityQueue<>();private Object locker = new Object();public MyTimer() {Thread t = new Thread(() -> {while (true) {try {synchronized (locker) {if (queue.isEmpty()) {locker.wait();}MyTimerTask task = queue.peek();long curTime = System.currentTimeMillis();if (curTime < task.getTime()) {locker.wait(task.getTime() - curTime);} else {task.getTask().run();queue.poll();}}} catch (InterruptedException e) {e.printStackTrace();}}});t.start();}//向定时器中添加任务public void schedule(Runnable runnable, long delay) {synchronized (locker) {queue.offer(new MyTimerTask(runnable, delay));locker.notify();}}
}public class Demo2 {public static void main(String[] args) {MyTimer timer = new MyTimer();timer.schedule(new Runnable() {@Overridepublic void run() {System.out.println("定时任务:3000");}}, 3000);timer.schedule(new Runnable() {@Overridepublic void run() {System.out.println("定时任务:2000");}}, 2000);timer.schedule(new Runnable() {@Overridepublic void run() {System.out.println("定时任务:1000");}}, 1000);}
}
http://www.dtcms.com/a/449131.html

相关文章:

  • 网站搭建介绍网站建设的原因
  • 怎么建免费网站建设公司网站新闻宣传管理制度
  • Deep Code Research:当 Deep Research 遇上 ABCoder
  • JavaEE初阶——中秋特辑:网络编程送祝福从 Socket 基础到 TCP/UDP 实战
  • 多模卫星导航定位与应用-原理与实践(RTKLib)3
  • 数字婵娟:一部关于中秋节的计算主义宣言
  • ED2K技术
  • 【数据结构】顺序表0基础知识讲解 + 实战演练
  • GPU即服务:Linux与云原生如何联手开启AI算力“自来水“时代
  • 【数据结构】算法复杂度
  • 校园网门户网站建设招聘网站如何做
  • 深度学习(十六):数据归一化处理
  • 力扣70.爬楼梯
  • 【深度学习计算机视觉】10:转置卷积
  • 电子商务网站策划素材网站 模板
  • Coze源码分析-资源库-编辑知识库-后端源码-安全/错误处理机制
  • 【无标题】标签单击事件
  • GAMES101:现代计算机图形学入门(Chapter5 光栅化1(三角形遍历))迅猛式学习笔记(附Homework 0)
  • 【Linux操作系统】进程概念
  • 【Linux】Linux进程信号(上)
  • 海思SS528/22AP30开发笔记之环境搭建和SDK编译
  • 算法二分法详解
  • 信号 | 基本描述 / 分类 / 运算
  • 【环境配置 升级gcc】RK3588 Ubuntu20.04 gcc9升级为gcc10
  • 资产信息收集与指纹识别:HTTPX联动工具实战指南
  • 鼠标消息超时处理——实现图形界面自动操作,避免鼠标消息阻塞
  • 用AI帮忙,开发刷题小程序:微信小程序在线答题系统架构解析
  • 用AI帮忙,开发刷题小程序:从零开始,构建微信小程序答题系统
  • 简单一点的网站建设个人网页设计页眉
  • 生成式人工智能赋能高中物理教学:范式转型、实践路径与效果评估