调用方法:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
|
/** * 点击量/月(年)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(); } |
线程方法:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
|
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); } } |
同样给大家分享下网友的实例
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
|
package JavaThread; class firstThread extends Thread { private String name = null ; public firstThread(String str) { this .name = str; } public void run() { for ( int i= 1 ;i<= 3 ;i++) { System.out.println( "线程" + this .name+ "第" +i + "执行" ); try { Thread.sleep( 50 ); } catch (InterruptedException e) { e.printStackTrace(); } } } } class secondThread extends Thread { private String name = null ; public secondThread(String s) { this .name = s; } public void run() { for ( int i= 1 ;i<= 3 ;i++) { System.out.println( "线程" + this .name+ "第" +i + "执行" ); try { Thread.sleep( 50 ); Thread.yield(); } catch (InterruptedException e) { e.printStackTrace(); } } } } public class TestThread { public static void main(String[] args) { firstThread p = new firstThread( "first" ); secondThread pth = new secondThread( "second" ); p.setPriority( 4 ); pth.setPriority( 9 ); p.start(); pth.start(); } } |
简单讲下继承Thread类
步骤:
a,定义类继承Thread类。
b,覆盖Thread类中的run方法,将需要被多线程执行的代码定义到该run方法当中。
c,建立Thread类的子类创建线程对象。
d,调用start方法,开启线程并调用该线程的run方法。
下面有个示例来让你直观的了解怎么用继承Thread类的方式来创建线程。
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
|
/* * 示例:创建三个线程,每过2秒打印一下线程的名称,打印三次 */ public class Thread1 extends Thread{ private final int MAX = 3 ; //最大打印次数 private int COUNT = 1 ; //计数 private final int TIME = 2 ; //间隔时间 //接收线程名称 public Thread1(String name) { super (name); } //覆盖run方法,在里面写我们要执行的代码 public void run() { while (COUNT<= MAX){ System.out.println( this .getName()); COUNT++; //每次打印后,在一段时间后再打印 try { Thread.sleep(TIME* 1000 ); } catch (InterruptedException e) { e.printStackTrace(); } } } public static void main(String[] args) { Thread1 t1 = new Thread1( "线程1" ); //创建线程 Thread1 t2 = new Thread1( "线程2" ); Thread1 t3 = new Thread1( "线程3" ); t1.start(); //开启线程 t2.start(); t3.start(); //也可以使用下面这种方式书写 //new Thread1("线程4").start(); } } |