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

实现阻塞队列

import java.util.concurrent.ArrayBlockingQueue;
import java.util.concurrent.BlockingQueue;
import java.util.concurrent.LinkedBlockingQueue;

public class Main  {
    public static void main(String[] args) throws InterruptedException {
        BlockingQueue<String> queue = new LinkedBlockingQueue<>();
        queue.put("111");
        queue.put("222");
        queue.put("333");
        queue.put("444");
        String elem = queue.take();
        System.out.println(elem);
        elem = queue.take();
        System.out.println(elem);
        elem = queue.take();
        System.out.println(elem);
        elem = queue.take();
        System.out.println(elem);
        elem = queue.take();
        System.out.println(elem);
    }
}

// 直接让这个队列里面存储字符串.
class MyBlockingQueue {
    // 此处这里的最大长度, 也可以指定构造方法, 由构造方法的参数来制定.
    private String[] data = new String[1000];
    // 队列的起始位置.
    private volatile int head = 0;
    // 队列的结束位置的下一个位置.
    private volatile int tail = 0;
    // 队列中有效元素的个数.
    private volatile int size = 0;

    // private final Object locker = new Object();

    // 提供核心方法, 入队列和出队列.
    public void put(String elem) throws InterruptedException {
        synchronized (this) {
            while (size == data.length) {
                // 队列满了.
                // 如果是队列满, 继续插入元素, 就会阻塞.
                this.wait();
            }
            // 队列没满, 真正的往里面添加元素
            data[tail] = elem;
            tail++;
            // 如果 tail 自增之后, 到达了数组末尾. 这个时候就需要让它回到开头 (环形队列)
            if (tail == data.length) {
                tail = 0;
            }
            size++;
            // 这个 notify 用来唤醒 take 中的 wait
            this.notify();
        }
    }

    public String take() throws InterruptedException {
        synchronized (this) {
            while (size == 0) {
                // 队列空了.
                this.wait();
            }
            // 队列不空, 就可以把队首元素 (head 位置的元素) 删除掉, 并进行返回.
            String ret = data[head];
            head++;
            if (head == data.length) {
                head = 0;
            }
            size--;
            // 这个 notify 用来唤醒 put 中的 wait
            this.notify();
            return ret;
        }
    }
}

public class Main {
    public static void main(String[] args) {
        // 生产者, 消费者, 分别使用一个线程表示. (也可以使用多个线程)

        MyBlockingQueue queue = new MyBlockingQueue();

        // 消费者
        Thread t1 = new Thread(() -> {
            while (true) {
                try {
                    String result = queue.take();
                    System.out.println("消费元素: " + result);

                    // 暂时先不 sleep
                    Thread.sleep(500);
                } catch (InterruptedException e) {
                    throw new RuntimeException(e);
                }
            }
        });

        // 生产者
        Thread t2 = new Thread(() -> {
            int num = 1;
            while (true) {
                try {
                    queue.put(num + "");
                    System.out.println("生产元素: " + num);
                    num++;
                } catch (InterruptedException e) {
                    throw new RuntimeException(e);
                }
            }
        });

        t1.start();
        t2.start();
    }
}
import java.util.Timer;
import java.util.TimerTask;

// 定时器
public class Main{
    public static void main(String[] args) {
        Timer timer = new Timer();
        // 给定时器安排了一个任务, 预定在 xxx 时间去执行.
        timer.schedule(new TimerTask() {
            @Override
            public void run() {
                System.out.println("3000");
            }
        }, 3000);
        timer.schedule(new TimerTask() {
            @Override
            public void run() {
                System.out.println("2000");
            }
        }, 2000);
        timer.schedule(new TimerTask() {
            @Override
            public void run() {
                System.out.println("1000");
            }
        }, 1000);
        System.out.println("程序启动!");
    }
}

import java.util.PriorityQueue;

// 通过这个类, 描述了一个任务
class MyTimerTask implements Comparable<MyTimerTask> {
    // 要有一个要执行的任务
    private Runnable runnable;
    // 还要有一个执行任务的时间
    private long time;

    // 此处的 delay 就是 schedule 方法传入的 "相对时间"
    public MyTimerTask(Runnable runnable, long delay) {
        this.runnable = runnable;
        this.time = System.currentTimeMillis() + delay;
    }

    @Override
    public int compareTo(MyTimerTask o) {
        // 这样的写法, 就是让队首元素是最小时间的值
        // 到底是谁 - 谁, 不要背!! 你可以试试!!
        return (int) (this.time - o.time);
        // 如果是想让队首元素是最大时间的值
        // return o.time - this.time;
    }

    public long getTime() {
        return time;
    }

    public Runnable getRunnable() {
        return runnable;
    }
}

// 自己搞的定时器
class MyTimer {
    // 使用一个数据结构, 保存所有要安排的任务.
    private PriorityQueue<MyTimerTask> queue = new PriorityQueue<>();
    // 使用这个对象作为锁对象.
    private Object locker = new Object();

    public void schedule(Runnable runnable, long delay) {
        synchronized (locker) {
            queue.offer(new MyTimerTask(runnable, delay));
            locker.notify();
        }
    }

    // 搞个扫描线程.
    public MyTimer() {
        // 创建一个扫描线程
        Thread t = new Thread(() -> {
            // 扫描线程, 需要不停的扫描队首元素, 看是否是到达时间.
            while (true) {
                try {
                    synchronized (locker) {
                        // 不要使用 if 作为 wait 的判定条件, 应该使用 while
                        // 使用 while 的目的是为了在 wait 被唤醒的时候, 再次确认一下条件.
                        while (queue.isEmpty()) {
                            // 使用 wait 进行等待.
                            // 这里的 wait, 需要由另外的线程唤醒.
                            // 添加了新的任务, 就应该唤醒.
                            locker.wait();
                        }
                        MyTimerTask task = queue.peek();
                        // 比较一下看当前的队首元素是否可以执行了.
                        long curTime = System.currentTimeMillis();
                        if (curTime >= task.getTime()) {
                            // 当前时间已经达到了任务时间, 就可以执行任务了
                            task.getRunnable().run();
                            // 任务执行完了, 就可以从队列中删除了.
                            queue.poll();
                        } else {
                            // 当前时间还没到任务时间, 暂时不执行任务.
                            // 暂时先啥都不干, 等待下一轮的循环判定了.
                            locker.wait(task.getTime() - curTime);
                        }
                    }
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        });
        t.start();
    }
}

public class Main {
    public static void main(String[] args) {
        MyTimer timer = new MyTimer();
        timer.schedule(new Runnable() {
            @Override
            public void run() {
                System.out.println("3000");
            }
        }, 3000);
        timer.schedule(new Runnable() {
            @Override
            public void run() {
                System.out.println("2000");
            }
        }, 2000);
        timer.schedule(new Runnable() {
            @Override
            public void run() {
                System.out.println("1000");
            }
        }, 1000);
        System.out.println("程序开始执行");
    }
}
 

http://www.lryc.cn/news/326730.html

相关文章:

  • MySQL8.X驱动datetime映射问题
  • 【Selenium】隐藏元素的定位和操作|隐藏与isDisplay方法
  • 视图的作用
  • 动态ip白名单频繁更改问题解决方案
  • 什么是物联网监控平台?部署物联网平台有什么作用?
  • netty构建udp服务器以及发送报文到客户端客户端详细案例
  • Selenium 学习(0.22)——软件测试之小结
  • 贪心算法问题
  • 深入理解 @Transactional 注解在 Spring 中的应用
  • Python爬虫之爬取网页图片
  • AI Agent(LLM Agent)入门解读
  • 自动化面试常见算法题!
  • CCF-CSP真题202206-2《寻宝!大冒险!》
  • Rust编程(三)生命周期与异常处理
  • 【办公类-21-11】 20240327三级育婴师 多个二级文件夹的docx合并成docx有页码,转PDF
  • OSG编程指南<二十一>:OSG视图与相机视点更新设置及OSG宽屏变形
  • Laplace变换-3
  • LVS负载均衡-DR模式配置
  • 【unity】如何汉化unity Hub
  • 【算法】KMP-快速文本匹配
  • 多维数组和交错数组笔记
  • Python(django)之单一接口展示功能前端开发
  • 【大模型】非常好用的大语言模型推理框架 bigdl-llm,现改名为 ipex-llm
  • Kubernetes示例yaml:3. service-statefulset.yaml
  • Windows平台cmake编译QT源码库,使用VScode开发QT
  • 腾讯云轻量8核16G18M服务器多少钱一年?
  • 二分练习题——123
  • 淘宝详情数据采集(商品上货,数据分析,属性详情,价格监控),海量数据值得get
  • Django之Web应用架构模式
  • GPT提示词分享 —— 口播脚本