一区二区三区在线-一区二区三区亚洲视频-一区二区三区亚洲-一区二区三区午夜-一区二区三区四区在线视频-一区二区三区四区在线免费观看

服務(wù)器之家:專注于服務(wù)器技術(shù)及軟件下載分享
分類導(dǎo)航

PHP教程|ASP.NET教程|JAVA教程|ASP教程|

服務(wù)器之家 - 編程語(yǔ)言 - JAVA教程 - Java多線程繼承Thread類詳解

Java多線程繼承Thread類詳解

2020-05-18 12:31java教程網(wǎng) JAVA教程

Java多線程的兩種實(shí)現(xiàn)方式:繼承Thread類 & 實(shí)現(xiàn)Runable接口,今天我們來(lái)學(xué)習(xí)下繼承Thread類,希望大家能夠喜歡

調(diào)用方法:

?
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
/**
 * 點(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();
 }

線程方法:

?
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;
 
/**
 * 多線程無(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);
 
 }
 
}

 

同樣給大家分享下網(wǎng)友的實(shí)例

?
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 +"執(zhí)行");
   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 +"執(zhí)行");
   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();
 }
 
}

簡(jiǎn)單講下繼承Thread類

                步驟:
                a,定義類繼承Thread類。
                b,覆蓋Thread類中的run方法,將需要被多線程執(zhí)行的代碼定義到該run方法當(dāng)中。
                c,建立Thread類的子類創(chuàng)建線程對(duì)象。
                d,調(diào)用start方法,開啟線程并調(diào)用該線程的run方法。

        下面有個(gè)示例來(lái)讓你直觀的了解怎么用繼承Thread類的方式來(lái)創(chuàng)建線程。

?
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
/*
* 示例:創(chuàng)建三個(gè)線程,每過(guò)2秒打印一下線程的名稱,打印三次
*/
public class Thread1 extends Thread{
  private final int MAX = 3;//最大打印次數(shù)
  private int COUNT = 1;//計(jì)數(shù)
  private final int TIME = 2;//間隔時(shí)間
 
  //接收線程名稱
  public Thread1(String name) {
    super(name);
  }
  //覆蓋run方法,在里面寫我們要執(zhí)行的代碼
  public void run() {
    while(COUNT<= MAX){
      System.out.println(this.getName());
      COUNT++;
      //每次打印后,在一段時(shí)間后再打印
      try {
        Thread.sleep(TIME*1000);
      } catch (InterruptedException e) {
        e.printStackTrace();
      }
    }
  }
  public static void main(String[] args) {
    Thread1 t1 = new Thread1("線程1");//創(chuàng)建線程
    Thread1 t2 = new Thread1("線程2");
    Thread1 t3 = new Thread1("線程3");
    t1.start(); //開啟線程
    t2.start();
    t3.start();
    //也可以使用下面這種方式書寫
    //new Thread1("線程4").start();
  }
}

 

延伸 · 閱讀

精彩推薦
主站蜘蛛池模板: 日韩小视频在线观看 | 全肉一女n男np高h双龙养成 | sss视频在线精品 | 精品久久一 | 日本免费在线观看 | 末代皇帝无删减版在线观看 | 日本大学生xxxxx69泡妞 | 亚洲va久久久噜噜噜久久狠狠 | 777午夜精品免费播放 | 男人添女人 | 第一次破女视频国产一级 | 日韩精品欧美国产精品亚 | 成人福利免费在线观看 | 欧美一级欧美三级在线 | 高清国产精品久久久久 | 日韩在线二区全免费 | aaa一级毛片免费 | 国产hd老头老太婆 | 日韩精品久久不卡中文字幕 | 亚欧毛片基地国产毛片基地 | ova催眠性指导5最新在线 | 久久国产免费 | 91对白在线 | 亚洲天堂色图 | 美女靠逼的视频 | 黄动漫车车好快的车车双女主 | 午夜在线观看视频 | heyzo在线播放 | 亚洲成在人线视频 | 午夜一个人在线观看完整版 | 色777777女人色| 精品国产麻豆AV无码 | 国产精品福利短视在线播放频 | 午夜在线观看免费观看 视频 | 无码欧美喷潮福利XXXX | 久久受www免费人成_看片中文 | 99热久久这里只有精品23 | 日本高清在线看免费观看 | 午夜电影三级还珠格格 | 国产午夜免费不卡精品理论片 | 免费精品在线 |