您当前的位置: 首页 >  Java

郭梧悠

暂无认证

  • 0浏览

    0关注

    402博文

    0收益

  • 0浏览

    0点赞

    0打赏

    0留言

私信
关注
热门博文

Java并发编程实战之基于生产者消费者模式的日志服务读书笔记

郭梧悠 发布时间:2021-11-06 16:59:58 ,浏览量:0

日常开发中,我们会经常跟日志系统打交道,日志系统算是典型的生产者消费者模型的系统。(多个不同的)生产者线程负责生产日志消息,这些系统扮演者生产者的角色,日志系统负责将消息队列里的日志上传到服务器,属于消费者角色。是一种多生产者单消费者模式的设计方式。如果生产者的生产速度大于LoggerThread 的消费速度,则BlockingDeque线程会阻塞生产者,直到LoggerThread 有能力处理新的消息。

不支持关闭的生产者-消费者日志服务(bug版本)
public class LogService {
    private final BlockingQueue queue;
    private final LoggerThread loggerThread;

    public LogService() {
        //注意队列的容量为10
        queue = new LinkedBlockingQueue(10);
        loggerThread = new LoggerThread();
    }
    public void start() {
        loggerThread.start();
    }

    /**
     * 生产者生产消息,每个log的调用者都行相当于一个生产者
     *
     * @param msg
     * @throws InterruptedException
     */
    public void log(String msg) throws InterruptedException {
        queue.put(msg);
    }

    /**
     * 消费者线程消费消息,该线程扮演的是消费者这一角色。
     */
    private class LoggerThread extends Thread {

       public  LoggerThread(){
        	
        }
        public void run() {
            try {
                while (true) {//无限循环
                   System.out.println(queue.take());
                }//end while
            } catch (InterruptedException e) {//在循环外响应中断
            } finally {
            	System.out.println("log service is close");
            }
        }
      }
    }

上面代码看起来很正常,但是有如下缺点: 1、LoggerThread没有提供关闭的方法,当没有生产者生产消息的时候,那么LoggerThread在调用queue.take()的时候会一直处于阻塞状态,导致JVM没法正常关闭。比如下面的调用代码,就会发生LoggerThread无法关闭的情况。

 public static void main(String[] args) {
    	LogService logService = new LogService();
    
    	for(int i=0;i            
关注
打赏
1663674776
查看更多评论
0.0382s