国产片侵犯亲女视频播放_亚洲精品二区_在线免费国产视频_欧美精品一区二区三区在线_少妇久久久_在线观看av不卡

服務器之家:專注于服務器技術及軟件下載分享
分類導航

PHP教程|ASP.NET教程|Java教程|ASP教程|編程技術|正則表達式|C/C++|IOS|C#|Swift|Android|VB|R語言|JavaScript|易語言|vb.net|

服務器之家 - 編程語言 - Java教程 - 實例總結Java多線程編程的方法

實例總結Java多線程編程的方法

2021-06-03 11:17Java之家 Java教程

在本篇文章里我們給大家總結了Java多線程編程的方法以及相關實例代碼,需要的朋友們可以學習下。

1.什么時候使用多線程編程

一個任務在正常情況下是按順序執(zhí)行的,但是如果當前任務里有多個相似進程塊(例如for,while語句),我們就可以考慮把這些代碼塊抽出來并行運行,無需阻塞

2.實現(xiàn)多線程的幾種方式

一種是繼承thread類重寫run方法,另一種是實現(xiàn)runnable接口重寫run方法

啟動多線程很多情況下是為了處理并發(fā)進程,此時對于部分實時性要求不是那么高的業(yè)務需求,我們還可以通過實現(xiàn)隊列的方式,異步實現(xiàn)。

3.舉例

繼承thread

?
1
2
3
4
5
6
7
8
9
10
11
12
13
/**
 *
* @classname: threadbyex
* @description: todo
* @author mr.jqcheng
* @date 2018年9月26日
* */public class threadbyex extends thread{
 
  @override  public void run() {    // todo auto-generated method stub
    system.out.println("我是繼承線程");
  }
 
}

實現(xiàn)runnable

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
/**
 *
* @classname: threadbyrunnable
* @description: todo
* @author mr.jqcheng
* @date 2018年9月26日
* */public class threadbyrunnable implements runnable{  /*public threadbyrunnable() {
    this.run();
    // todo auto-generated constructor stub
  }*/
 
  public void run() {    // todo auto-generated method stub
    system.out.println("我是實現(xiàn)進程");
  }
 
}

測試:

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
/**
 *
* @classname: test
* @description: todo
* @author mr.jqcheng
* @date 2018年9月26日
* */public class test {  public static void main(string[] args) {    // 繼承thread啟動的方法
    threadbyex t1 = new threadbyex();
    t1.start();// 啟動線程    // 實現(xiàn)runnable啟動線程的方法
    threadbyrunnable r = new threadbyrunnable();
    thread t2 = new thread(r);
    t2.start();// 啟動線程    //new threadbyrunnable();  }
 
}

運行結果:

我是繼承線程

我是實現(xiàn)進程

ok,簡單的多線程實現(xiàn)方式完成了,在調用start()的時候,該進程已經進入可執(zhí)行狀態(tài),等待系統(tǒng)執(zhí)行。

線程處理的幾個常用方法:

void interrupt():向線程發(fā)送中斷請求,線程的中斷狀態(tài)將會被設置為true,如果當前線程被一個sleep調用阻塞,那么將會拋出interrupedexception異常。

static boolean interrupted():測試當前線程(當前正在執(zhí)行命令的這個線程)是否被中斷。注意這是個靜態(tài)方法,調用這個方法會產生一個副作用那就是它會將當前線程的中斷狀態(tài)重置為false。

boolean isinterrupted():判斷線程是否被中斷,這個方法的調用不會產生副作用即不改變線程的當前中斷狀態(tài)。

static thread currentthread() : 返回代表當前執(zhí)行線程的thread對象。

守護進程

用來服務于不是服務進程的其他所有當前進程下的所有線程

實現(xiàn)deamon.setdaemon(true)就行,要在線程開啟之前啟用

舉例

?
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
package com.orange.util;
/**
 *
 * @classname: test
 * @description: todo
 * @author mr.jqcheng
 * @date 2018年9月26日
 *
 */
public class test {
  public static void main(string[] args) {
    thread deamon2 = new thread(new daemonrunner2(), "otherrunner");
    deamon2.start();// 啟動線程
    try {
      thread.sleep(1000);
    } catch (interruptedexception e) {
      // todo auto-generated catch block
      e.printstacktrace();
    }
    thread deamon = new thread(new daemonrunner(), "daemonrunner");
    // 設置為守護線程
    deamon.setdaemon(true);
    deamon.start();// 啟動線程
  }
  static class daemonrunner implements runnable {
    public void run() {
      // todo auto-generated method stub
      try {
        thread.sleep(300);
        thread t = thread.currentthread();
        system.out.println(t);
      } catch (exception e) {
        e.printstacktrace();
      } finally {
        system.out.println("進入守護線程,說明現(xiàn)在還有其他線程在執(zhí)行");
      }
    }
  }
  static class daemonrunner2 implements runnable {
    public void run() {
      // todo auto-generated method stub
      try {
        thread.sleep(1500);
        system.out.println("我是其他線程");
      } catch (exception e) {
        e.printstacktrace();
      }
    }
  }
}

執(zhí)行結果:

thread[daemonrunner,5,main]

進入守護線程,說明現(xiàn)在還有其他線程在執(zhí)行

我是其他線程

首先,先啟動其他線程,需要耗時1500ms,同時,主線程耗時1000ms后,開始進入守護線程,此時其它線程還在運行,到了守護線程,耗時300ms,其他線程仍在執(zhí)行,繼續(xù)往下,守護線程執(zhí)行完畢

但是如果我把守護線程的300ms改成500ms,會發(fā)生什么事呢?

出現(xiàn)過兩種情況,畢竟在臨界值

1.我是其他線程

2.thread[daemonrunner,5,main]

進入守護線程,說明現(xiàn)在還有其他線程在執(zhí)行

我是其他線程

延伸 · 閱讀

精彩推薦
Weibo Article 1 Weibo Article 2 Weibo Article 3 Weibo Article 4 Weibo Article 5 Weibo Article 6 Weibo Article 7 Weibo Article 8 Weibo Article 9 Weibo Article 10 Weibo Article 11 Weibo Article 12 Weibo Article 13 Weibo Article 14 Weibo Article 15 Weibo Article 16 Weibo Article 17 Weibo Article 18 Weibo Article 19 Weibo Article 20 Weibo Article 21 Weibo Article 22 Weibo Article 23 Weibo Article 24 Weibo Article 25 Weibo Article 26 Weibo Article 27 Weibo Article 28 Weibo Article 29 Weibo Article 30 Weibo Article 31 Weibo Article 32 Weibo Article 33 Weibo Article 34 Weibo Article 35 Weibo Article 36 Weibo Article 37 Weibo Article 38 Weibo Article 39 Weibo Article 40
主站蜘蛛池模板: 色av影院| 欧美精品一区二区三区在线播放 | 国产亚洲精品精品国产亚洲综合 | 国产片免费 | av网址在线 | 91免费视频 | 毛片免费观看网址 | 天天干天天干天天干天天射 | 久久久www免费人成精品 | 国产一区二区在线免费观看 | 欧美aaa级 | 一区二区视频 | 亚洲欧美视频播放 | 成人高清在线 | 日韩黄网站 | 性欧美久久久 | 久久99精品久久久久久噜噜 | 日本动漫一区 | 精品久久久久久久久久久下田 | 91精品一区二区三区久久久久久 | 一级欧美 | 中文字幕啪啪 | 在线色站 | 午夜精品久久久久久久久 | 国产成人久久精品一区二区三区 | 亚洲精品一区二区 | 欧美日韩一区二区在线观看 | 91免费网站| 国产一区二区三区久久 | 欧美专区在线观看 | 国产精品成人一区二区三区 | 欧美国产一区二区 | 日本久久免费 | 亚色在线 | 久久精品国产精品亚洲 | 欧美亚洲日本 | 亚洲第一成av人网站懂色 | 亚洲一区二区三区在线播放 | 久久99这里只有精品 | 天天影视色香欲 | 成人免费观看高清视频 |