1. 程式人生 > >java自帶消費

java自帶消費

mes sage exception 信息 args class ise bsp isempty

package com.csf.myproject.core;

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

import org.apache.log4j.Logger;

public class QueueFile {

    // 上傳信息
    private static final Logger logger = Logger.getLogger(QueueFile.class);

    // 快照行情
    private static
BlockingQueue<String> queue = new LinkedBlockingQueue<String>(); private static boolean isEOF = false; public static int count = 0; public static int getCount(){ return count; } // 放入行情 public static void produce(String message) { try { count
++; queue.put(message); } catch (Exception e) { logger.error(e, e); } } // 消費行情 public static String consume() { try { return queue.take(); } catch (Exception e) { logger.error(e, e); } return null
; } // 行情隊列是否為空 public static boolean isEmpty() { return (queue.isEmpty() && isEOF); } // 關閉隊列 public static void close() { QueueFile.isEOF = true; } public static void main(String[] args) { QueueFile.produce("hello"); QueueFile.produce("hell2o"); QueueFile.produce("hell2o"); for( int i = 0 ; i <= QueueFile.getCount();i ++){ String consume = QueueFile.consume(); System.out.println(consume); } } // 方式二 隊列消費 public static void main2(String[] args){ Queue<String> queue = new ConcurrentLinkedQueue<>(); int count = 0; queue.add("馬風雷"); queue.add("當前的測試文件"); queue.add("馬上消費"); count = 3; for(int i= 0;i< count;i++ ){ String mes = queue.poll(); System.out.println(mes); } } }

java自帶消費