jsmubanlogo
  • 首页
  • 网页模板
  • 特效代码
  • 博文源码
  • 插件下载
  •    

Java Queue 单线程队列的实现方式

收藏    

作者第十天    2022-02-26

      


Java Queue 单线程队列的实现方式,可用于发送消息、记录日志等操作。

import java.util.Queue;
import java.util.concurrent.LinkedBlockingDeque;
 
/**
 * Java 单线程 Queue 队列
 * 顺序执行,先进先出
 * @author sun
 * @date 2018年5月19日 下午12:05:27
 */
public class TaskQueue implements Runnable {
//  protected static final Logger LOG = Logger.getLogger(TaskQueue.class);
     
    private static TaskQueue taskQueue = null;// 单例
 
    public Queue<String> queue = new LinkedBlockingDeque<String>();
//  public Queue<Map<String, Object>> queue = new LinkedBlockingDeque<Map<String, Object>>();//可以用来处理日志等
 
    /**
     * 单例获取
     * @author sun
     * @date 2018年5月19日 下午12:09:42
     * @return
     */
    public static TaskQueue getInstance() {
        if (taskQueue == null) {
            taskQueue = new TaskQueue();
            new Thread(taskQueue).start();
        }
        return taskQueue;
    }
 
    /**
     * 添加任务,并且notify通知
     * @author sun
     * @date 2018年5月19日 下午12:09:30
     * @param task
     */
    public void addTask(String task) {
        queue.add(task);
        synchronized (queue) {
            try {
                //TODO 【正常情况下try块需要删除】
                //TODO 为了演示效果,这块延迟500毫秒执行、打印。
                System.out.println("线程唤醒");
                Thread.sleep(500);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            queue.notify();//唤醒
        }
    }
 
    public void run() {
        while (true) {
            getTask();
        }
    }
 
    /**
     * 监听获取任务
     * @author sun
     * @date 2018年5月19日 下午12:09:22
     */
    private synchronized void getTask() {
        String task = null;
        synchronized (queue) {
            /** 用于删除第一个头部元素,并且返回第一个头部元素,没有则返回null **/
            task = queue.poll();
        }
        if (task == null) {
            try {
                synchronized (queue) {
                    try {
                        //TODO 【正常情况下try块需要删除】
                        //TODO 为了演示效果,这块延迟500毫秒执行、打印。
                        Thread.sleep(500);
                        System.out.println("线程睡眠,等待被唤醒");
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                    queue.wait();// 继续等待
                }
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        } else {
            doTask(task);//去执行任务
        }
    }
 
    /**
     * 执行获取到的任务
     * @author sun
     * @date 2018年5月19日 下午12:09:59
     * @param task
     */
    private void doTask(String task) {
        System.err.println("任务执行:"+task);
    }
     
    /**
     * 测试
     * @author sun
     * @date 2018年5月19日 下午12:10:18
     * @param args
     */
    public static void main(String[] args) {
        TaskQueue taskq = TaskQueue.getInstance();
        for(int i=0;i<10;i++){
            try {
                Thread.sleep(1000);
                taskq.addTask("任务编号["+i+"]");
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        } 
        try {
            Thread.sleep(3000);
            taskq.addTask("最后一个任务");
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
         
    }
     
}

控制台打印:

线程睡眠,等待被唤醒
线程唤醒
任务执行:任务编号[0]
线程睡眠,等待被唤醒
线程唤醒
任务执行:任务编号[1]
线程睡眠,等待被唤醒
线程唤醒
任务执行:任务编号[2]
线程睡眠,等待被唤醒
线程唤醒
任务执行:任务编号[3]
线程睡眠,等待被唤醒
线程唤醒
任务执行:任务编号[4]
线程睡眠,等待被唤醒
线程唤醒
任务执行:任务编号[5]
线程睡眠,等待被唤醒
线程唤醒
任务执行:任务编号[6]
线程睡眠,等待被唤醒
线程唤醒
任务执行:任务编号[7]
线程睡眠,等待被唤醒
线程唤醒
任务执行:任务编号[8]
线程睡眠,等待被唤醒
线程唤醒
任务执行:任务编号[9]
线程睡眠,等待被唤醒
线程唤醒
任务执行:最后一个任务
线程睡眠,等待被唤

版权属于: 技术客

原文地址: https://www.sunjs.com/article/detail/958c2171273b46eb86a45e145a278895.html


免责声明:
      1、 资源售价只是赞助,不代表代码或者素材本身价格。收取费用仅维持本站的日常运营所需。
      2、 本站资源来自用户上传,仅供用户学习使用,不得用于商业或者非法用途,违反国家法律一切后果用户自负。用于商业用途,请购买正版授权合法使用。
      3、 本站资源不保证其完整性和安全性,下载后自行检测安全,在使用过程中出现的任何问题均与本站无关,本站不承担任何技术及版权问题,不对任何资源负法律责任。
      4、 如有损害你的权益,请联系275551777@qq.com及时删除。

关于我们 | 积分获取 | 联系我们 | 用户协议 | 标签搜索 | 网站地图.html | 网站地图.xml | 网站地图.txt

Copyright © 2021-2023 All Right Reserved
陕公网安备 61082202000148号      陕ICP备2025078528号-1
js模板网 -陕西千手码农科技有限责任公司