激情久久久_欧美视频区_成人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)方式完成了,在調(diào)用start()的時候,該進程已經(jīng)進入可執(zhí)行狀態(tài),等待系統(tǒng)執(zhí)行。

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

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

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

boolean isinterrupted():判斷線程是否被中斷,這個方法的調(diào)用不會產(chǎn)生副作用即不改變線程的當前中斷狀態(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í)行

我是其他線程

延伸 · 閱讀

精彩推薦
主站蜘蛛池模板: 久久伊 | 国产亚洲精品综合一区 | 亚洲一区二区三区高清视频 | 亚洲综人网 | 成人aaaaa片毛片按摩 | 在线 日本 制服 中文 欧美 | 久久精品之 | 污黄视频在线播放 | 777sesese| 成人偷拍片视频在线观看 | 午夜热门福利 | 日本免费aaa观看 | 国产精品美女久久久免费 | 一区二区三区视频在线播放 | 国产一区精品在线观看 | 欧美精品一级 | 欧美日韩精品中文字幕 | a集毛片 | 欧美hdfree性xxxx | 狠狠ri| 亚洲性生活免费视频 | 欧美激情综合在线 | 国产91丝袜在线熟 | 99爱视频在线 | 免费在线观看成人av | 黄色电影免费网址 | 国产在线欧美日韩 | 激情午夜天 | 在线播放免费播放av片 | 国产精品成人久久 | 在线视频观看一区二区 | 久久综合入口 | 一色桃子av大全在线播放 | 中文区永久区 | 欧美成人一区免费视频 | 欧美一级特黄特色大片免费 | 羞羞草视频| asian gaysex| 特级西西444www大精品视频免费看 | 成人国产精品免费 | 高清国产免费 |