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

首頁 > 編程 > Java > 正文

Java多線程繼承Thread類詳解第1/2頁

2019-11-26 14:10:29
字體:
來源:轉載
供稿:網友

調用方法:

/** * 點擊量/月(年)Thread */ public void yearlyClickThread() { // 獲取參數 String year = getPara("year"); // 統計數據集X List<String> xList = new ArrayList<String>(); xList.add("January"); xList.add("February"); xList.add("March"); xList.add("April"); xList.add("May"); xList.add("June"); xList.add("July"); xList.add("August"); xList.add("September"); xList.add("October"); xList.add("November"); xList.add("December"); // 統計數據集Y List<Integer> yList = new ArrayList<Integer>(); // 統計線程狀態 List<Thread> threadList = new ArrayList<Thread>(); // 線程狀態碼 int threadStatusCode = 0; // 計數器 int count = 0; // 每月的日志分析 for (int m = 1; m <= 12; m++) { // 收集日期參數 List<String> dateList = new ArrayList<String>(); // String date = ""; // 判斷有多少天 int days = CalendarUtil.weekForMonth(Integer.valueOf(year), m); // 組合日期 for (int i = 1; i <= days; i++) { if (i <= 9) {  if (m <= 9) {  date = year + "-0" + m + "-0" + i;  } else {  date = year + "-" + m + "-0" + i;  } } else {  if (m <= 9) {  date = year + "-0" + m + "-" + i;  } else {  date = year + "-" + m + "-" + i;  } } dateList.add(date); } // 啟動線程 Thread thread = new ReadLogFileThreadByYear(dateList); thread.start(); try { // 休眠 Thread.sleep(1000L); } catch (InterruptedException e) { e.printStackTrace(); } threadList.add(thread); } // 獲取線程狀態 for (Thread t : threadList) { if (t.getState().toString().equals("TERMINATED")) { threadStatusCode += 1; } } // 判斷線程是否都執行完畢 if (threadStatusCode == 12) { // 接收參數 // List<Map<String, Object>> list = ReadLogFileThread.list.subList(0, 12); List<Map<String, Object>> list = ReadLogFileThreadByYear.list; // 設置參數 for (int p = 0; p < list.size(); p++) { count += (int) list.get(p).get("clickCount"); if (list.get(p).get("month").equals("01")) {  yList.add((Integer) list.get(p).get("clickCount")); } else if (list.get(p).get("month").equals("02")) {  yList.add((Integer) list.get(p).get("clickCount")); } else if (list.get(p).get("month").equals("03")) {  yList.add((Integer) list.get(p).get("clickCount")); } else if (list.get(p).get("month").equals("04")) {  yList.add((Integer) list.get(p).get("clickCount")); } else if (list.get(p).get("month").equals("05")) {  yList.add((Integer) list.get(p).get("clickCount")); } else if (list.get(p).get("month").equals("06")) {  yList.add((Integer) list.get(p).get("clickCount")); } else if (list.get(p).get("month").equals("07")) {  yList.add((Integer) list.get(p).get("clickCount")); } else if (list.get(p).get("month").equals("08")) {  yList.add((Integer) list.get(p).get("clickCount")); } else if (list.get(p).get("month").equals("09")) {  yList.add((Integer) list.get(p).get("clickCount")); } else if (list.get(p).get("month").equals("10")) {  yList.add((Integer) list.get(p).get("clickCount")); } else if (list.get(p).get("month").equals("11")) {  yList.add((Integer) list.get(p).get("clickCount")); } else if (list.get(p).get("month").equals("12")) {  yList.add((Integer) list.get(p).get("clickCount")); } } }  setAttr("totalCount", count); setAttr("x", xList); setAttr("y", yList); renderJson(); }

線程方法:

package com.ninemax.util.loganalysis;import java.io.BufferedReader;import java.io.File;import java.io.FileInputStream;import java.io.IOException;import java.io.InputStreamReader;import java.util.ArrayList;import java.util.HashMap;import java.util.List;import java.util.Map;import com.ninemax.util.loganalysis.tool.ConstantUtil;/** * 多線程無返回值 *  * @author Darker * */public class ReadLogFileThreadByYear extends Thread { // 日期數組 private List<String> clickDate; // 共享數據 public static List<Map<String, Object>> list = new ArrayList<Map<String, Object>>();  public ReadLogFileThreadByYear(List<String> clickDate) { this.clickDate = clickDate; } /** * 讀取點擊日志文件 *  * 例子:article.click.2016-05-20.txt *  * @return */ public void run() { // 接收參數 Map<String, Object> map = new HashMap<String, Object>(); // 利用FileInputStream讀取文件信息 FileInputStream fis = null; // 利用InputStreamReader進行轉碼 InputStreamReader reader = null; // 利用BufferedReader進行緩沖 BufferedReader bufReader = null; // 利用StringBuffer接收文件內容容器 StringBuffer buf = new StringBuffer(); // 點擊量/月 int monthClick = 0;  for (int i = 0; i < clickDate.size(); i++) { // 獲取文件 File clickLogFile = new File(ConstantUtil.LOGLOCATION, "article.click."+ clickDate.get(i) + ".txt"); // 判斷文件是否存在 if (!clickLogFile.exists() || clickLogFile.isDirectory()) { System.err.println(clickDate.get(i) + "的文件不存在..."); } else { try {  // 節點流  fis = new FileInputStream(clickLogFile);  // 轉換流  reader = new InputStreamReader(fis, "utf-8");  // 處理流  bufReader = new BufferedReader(reader);  // 計數器  int count = 0;  // 按行讀取  String line = "";  // 讀取文件  while ((line = bufReader.readLine()) != null) {  count++;  // 接收數據  if (!line.equals(null) && !line.equals("")) {  buf.append(line + "/n");  }  }  if (count == 0) {  count = 0;  } else {  count = count - 1;  }  monthClick += count; } catch (Exception e) {  e.printStackTrace(); } finally {  // 關閉流  try {  bufReader.close();  reader.close();  fis.close();  } catch (IOException e) {  e.printStackTrace();  } } } } map.put("month", clickDate.get(0).subSequence(5, 7)); if(monthClick==0){ map.put("clickCount", 0); }else{ map.put("clickCount", monthClick); }  // map.put("clickContent", buf.toString()); list.add(map); } }
發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 永兴县| 于都县| 德令哈市| 黄梅县| 鱼台县| 平原县| 广宗县| 靖边县| 博野县| 靖江市| 色达县| 咸宁市| 沙雅县| 齐河县| 桂东县| 邯郸市| 高阳县| 德兴市| 利川市| 德保县| 浑源县| 怀柔区| 松滋市| 西峡县| 来安县| 甘南县| 屏南县| 长汀县| 鄂尔多斯市| 鄂尔多斯市| 井研县| 米泉市| 饶阳县| 淮南市| 阿尔山市| 名山县| 安泽县| 栖霞市| 三台县| 兴义市| 浦东新区|