国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 學院 > 開發設計 > 正文

Java-BlockingQueue的使用

2019-11-15 00:51:58
字體:
來源:轉載
供稿:網友
java-BlockingQueue的使用

每次都是隔很長時間才在博客中寫點什么,說自己忙吧,這是給自己的一個借口,其實呢還是懶啊。哎。。。

最近項目中有個對比的需求,需要從日志文件中獲取到參數,然后調用不同的API,進行結果的對比。但是不知用什么方式比較好,于是查了下jdk的手冊,發現了BlockingQueue這個好東西。

關于BlockingQueue的介紹,大家有興趣的可以自己看下:http://docs.Oracle.com/javase/7/docs/api/java/util/concurrent/LinkedBlockingQueue.html

需求呢其實很簡單就是將參數放置到Queue中,然后交由下一個策略去消費。剛開始時是通過不同的線程往隊列中存放數據,然后返回給下個服務一個BlockingQueue的對象,下一個策略從隊列中消費,code如下:

@Sup

  可是在實際運行時,由于日志比較大,下一個策略可能要等1hour或更長的時間才能開始處理,這明顯是不符合要求的,于是又優化了下,將BlockingQueue改為全局static的,然后下一個策略可以直接監控這個隊列中是否有值,有值就消費,沒值就阻塞線程等待或者超時等其他處理。

改進后的code:

1、新建一個隊列類:

public class ParameterQueue extends LinkedBlockingQueue<InputOutputPrameters> {/** *@Fields serialVersionUID: */private static final long serialVersionUID = 6032356446145302484L;private static BlockingQueue<InputOutputPrameters> queue = new LinkedBlockingQueue<InputOutputPrameters>();/** * @Fields log: 日志記錄 */private static final Logger log = LoggerFactory.getLogger(ParameterQueue.class);/**  * 獲取隊列中的對象  * @Method:getParameter * @Description: 獲取隊列中的對象  * @return 獲取到的對象信息*/public static InputOutputPrameters getParameter(){InputOutputPrameters result = null;try {result  = (InputOutputPrameters)queue.take();} catch (Exception e) {log.error("獲取隊列異常,異常信息:" + e);}return result;}/**  * 獲取隊列的數量 * @Method:getQueueSize * @Description: 獲取隊列的數量  * @return 數量*/public static Integer getQueueSize() {return queue.size();}/**  * 放置參數到隊列中  * @Method:putParameter * @Description: 放置參數到隊列中  * @param parameter 要放置的對象*/public static void putParameter(InputOutputPrameters parameter) {try {queue.put(parameter);} catch (Exception e) {log.error("插入隊列異常,異常信息:" + e);}}}

  2、讀取文件時,直接操作該隊列,往隊列中put值,下一個策略從該隊列中get值,put的code如下:

public void getSource(String path) {try {File file = new File(path);BufferedReader reader = null;String tempStr = null;try {reader = new BufferedReader(new FileReader(file));while ((tempStr = reader.readLine()) != null) {final InputOutputPrameters parameter = new InputOutputPrameters();String[] list = tempStr.split(";");if (list != null && list.length > 0) {parameter.setInputParamters(list[0]);parameter.setOutputParameters(list[1]);}putInQueue(parameter);}reader.close();} catch (FileNotFoundException  e) {e.printStackTrace();} catch (IOException e) {e.printStackTrace();}finally { if (reader != null) { try { reader.close(); } catch (Exception e) { e.printStackTrace(); } } }} catch (Exception e) {log.error("系統異常: " + e);}}/**  * 將參數存放至隊列中  * @Method:putInQueue * @Description: 將參數存放至隊列中  * @param parameter 要存放的對象*/private void putInQueue(final InputOutputPrameters parameter) {new Thread(){public void run(){try {Thread.sleep((long)(Math.random()*100));log.info("開始存入數據!");ParameterQueue.putParameter(parameter);log.info("已經存入數據,目前隊列中有 " + ParameterQueue.getQueueSize() +" 個隊列!輸入參數:"+ parameter.getInputParamters() + ";/n輸出參數:" + parameter.getOutputParameters());} catch (Exception e) {log.error("系統異常:" + e);}}}.start();}

  

于是這個要求就達到了。記錄下這個小需求,方便以后查閱。

簡要說下,BlockingQueue是線程安全的,常用的是ArrayBlockingQueue、LinkedBlockingQueue

ArrayBlockingQueue需要制定容量,而LinkedBlockingQueue不需要

同時在消費時,take()是會阻塞線程的,如果是單線程跑時,take()不到時整個線程就卡了

所以看具體環境需求,是用take還是其他的,我一般用poll,因為可以制定超時時間。

哎 不知道怎么寫了,就這樣吧。


發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 石屏县| 阿鲁科尔沁旗| 汽车| 平邑县| 桂林市| 昌图县| 自治县| 仪征市| 南华县| 改则县| 喀喇| 周宁县| 宾阳县| 通许县| 独山县| 两当县| 盐源县| 竹溪县| 长子县| 梓潼县| 石林| 昌都县| 新巴尔虎左旗| 醴陵市| 兴安县| 上思县| 墨玉县| 海安县| 肇源县| 乡宁县| 宝坻区| 朝阳区| 虞城县| 曲阳县| 双柏县| 扎兰屯市| 石屏县| 湘潭县| 江阴市| 扬州市| 天津市|