Java多線程繼承Thread類詳解第1/2頁(yè)
更新時(shí)間:2016年06月19日 15:46:59 投稿:hebedich
Java多線程的兩種實(shí)現(xiàn)方式:繼承Thread類 & 實(shí)現(xiàn)Runable接口,今天我們來(lái)學(xué)習(xí)下繼承Thread類,希望大家能夠喜歡
調(diào)用方法:
/**
* 點(diǎn)擊量/月(年)Thread
*/
public void yearlyClickThread() {
// 獲取參數(shù)
String year = getPara("year");
// 統(tǒng)計(jì)數(shù)據(jù)集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");
// 統(tǒng)計(jì)數(shù)據(jù)集Y
List<Integer> yList = new ArrayList<Integer>();
// 統(tǒng)計(jì)線程狀態(tài)
List<Thread> threadList = new ArrayList<Thread>();
// 線程狀態(tài)碼
int threadStatusCode = 0;
// 計(jì)數(shù)器
int count = 0;
// 每月的日志分析
for (int m = 1; m <= 12; m++) {
// 收集日期參數(shù)
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);
}
// 啟動(dòng)線程
Thread thread = new ReadLogFileThreadByYear(dateList);
thread.start();
try {
// 休眠
Thread.sleep(1000L);
} catch (InterruptedException e) {
e.printStackTrace();
}
threadList.add(thread);
}
// 獲取線程狀態(tài)
for (Thread t : threadList) {
if (t.getState().toString().equals("TERMINATED")) {
threadStatusCode += 1;
}
}
// 判斷線程是否都執(zhí)行完畢
if (threadStatusCode == 12) {
// 接收參數(shù)
// List<Map<String, Object>> list = ReadLogFileThread.list.subList(0, 12);
List<Map<String, Object>> list = ReadLogFileThreadByYear.list;
// 設(shè)置參數(shù)
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;
/**
* 多線程無(wú)返回值
*
* @author Darker
*
*/
public class ReadLogFileThreadByYear extends Thread {
// 日期數(shù)組
private List<String> clickDate;
// 共享數(shù)據(jù)
public static List<Map<String, Object>> list = new ArrayList<Map<String, Object>>();
public ReadLogFileThreadByYear(List<String> clickDate) {
this.clickDate = clickDate;
}
/**
* 讀取點(diǎn)擊日志文件
*
* 例子:article.click.2016-05-20.txt
*
* @return
*/
public void run() {
// 接收參數(shù)
Map<String, Object> map = new HashMap<String, Object>();
// 利用FileInputStream讀取文件信息
FileInputStream fis = null;
// 利用InputStreamReader進(jìn)行轉(zhuǎn)碼
InputStreamReader reader = null;
// 利用BufferedReader進(jìn)行緩沖
BufferedReader bufReader = null;
// 利用StringBuffer接收文件內(nèi)容容器
StringBuffer buf = new StringBuffer();
// 點(diǎn)擊量/月
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 {
// 節(jié)點(diǎn)流
fis = new FileInputStream(clickLogFile);
// 轉(zhuǎn)換流
reader = new InputStreamReader(fis, "utf-8");
// 處理流
bufReader = new BufferedReader(reader);
// 計(jì)數(shù)器
int count = 0;
// 按行讀取
String line = "";
// 讀取文件
while ((line = bufReader.readLine()) != null) {
count++;
// 接收數(shù)據(jù)
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 {
// 關(guān)閉流
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);
}
}
相關(guān)文章
MyBatis?詳細(xì)講解動(dòng)態(tài)?SQL的使用
動(dòng)態(tài)?SQL?是?MyBatis?的強(qiáng)大特性之一。如果你使用過(guò)?JDBC?或其它類似的框架,你應(yīng)該能理解根據(jù)不同條件拼接?SQL?語(yǔ)句有多痛苦,例如拼接時(shí)要確保不能忘記添加必要的空格,還要注意去掉列表最后一個(gè)列名的逗號(hào)。利用動(dòng)態(tài)?SQL,可以徹底擺脫這種痛苦2022-04-04
java對(duì)象對(duì)比之comparable和comparator的區(qū)別
今天給大家?guī)?lái)的是關(guān)于Java的相關(guān)知識(shí),文章圍繞著comparable和comparator的區(qū)別展開(kāi),文中有非常詳細(xì)的介紹及代碼示例,需要的朋友可以參考下2021-06-06
Java SpringMVC攔截器與異常處理機(jī)制詳解分析
SpringMVC是一種基于Java,實(shí)現(xiàn)了Web MVC設(shè)計(jì)模式,請(qǐng)求驅(qū)動(dòng)類型的輕量級(jí)Web框架,即使用了MVC架構(gòu)模式的思想,將Web層進(jìn)行職責(zé)解耦?;谡?qǐng)求驅(qū)動(dòng)指的就是使用請(qǐng)求-響應(yīng)模型,框架的目的就是幫助我們簡(jiǎn)化開(kāi)發(fā),SpringMVC也是要簡(jiǎn)化我們?nèi)粘eb開(kāi)發(fā)2021-10-10
Springboot?HTTP如何調(diào)用其他服務(wù)
這篇文章主要介紹了Springboot?HTTP如何調(diào)用其他服務(wù),具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2022-01-01
java中JVM中如何存取數(shù)據(jù)和相關(guān)信息詳解
這篇文章主要介紹了JVM中如何存取數(shù)據(jù)和相關(guān)信息詳解,Java源代碼文件(.java后綴)會(huì)被Java編譯器編譯為字節(jié)碼文件,然后由JVM中的類加載器加載各個(gè)類的字節(jié)碼文件,加載完畢之后,交由JVM執(zhí)行引擎執(zhí)行。JVM中怎么存取數(shù)據(jù)和相關(guān)信息呢?,需要的朋友可以參考下2019-06-06
springboot+thymeleaf找不到視圖的解決方案
這篇文章主要介紹了springboot+thymeleaf找不到視圖的解決方案,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2021-06-06

