實(shí)例總結(jié)Java多線程編程的方法
1.什么時(shí)候使用多線程編程
一個(gè)任務(wù)在正常情況下是按順序執(zhí)行的,但是如果當(dāng)前任務(wù)里有多個(gè)相似進(jìn)程塊(例如for,while語句),我們就可以考慮把這些代碼塊抽出來并行運(yùn)行,無需阻塞
2.實(shí)現(xiàn)多線程的幾種方式
一種是繼承Thread類重寫run方法,另一種是實(shí)現(xiàn)Runnable接口重寫run方法
啟動(dòng)多線程很多情況下是為了處理并發(fā)進(jìn)程,此時(shí)對(duì)于部分實(shí)時(shí)性要求不是那么高的業(yè)務(wù)需求,我們還可以通過實(shí)現(xiàn)隊(duì)列的方式,異步實(shí)現(xiàn)。
3.舉例
繼承Thread
/** * * @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("我是繼承線程"); } }
實(shí)現(xiàn)Runnable
/** * * @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("我是實(shí)現(xiàn)進(jìn)程"); } }
測(cè)試:
/** * * @ClassName: Test * @Description: TODO * @author Mr.jqCheng * @date 2018年9月26日 * */public class Test { public static void main(String[] args) { // 繼承Thread啟動(dòng)的方法 ThreadByEx t1 = new ThreadByEx(); t1.start();// 啟動(dòng)線程 // 實(shí)現(xiàn)Runnable啟動(dòng)線程的方法 ThreadByRunnable r = new ThreadByRunnable(); Thread t2 = new Thread(r); t2.start();// 啟動(dòng)線程 //new ThreadByRunnable(); } }
運(yùn)行結(jié)果:
我是繼承線程
我是實(shí)現(xiàn)進(jìn)程
ok,簡(jiǎn)單的多線程實(shí)現(xiàn)方式完成了,在調(diào)用start()的時(shí)候,該進(jìn)程已經(jīng)進(jìn)入可執(zhí)行狀態(tài),等待系統(tǒng)執(zhí)行。
線程處理的幾個(gè)常用方法:
void interrupt():向線程發(fā)送中斷請(qǐng)求,線程的中斷狀態(tài)將會(huì)被設(shè)置為true,如果當(dāng)前線程被一個(gè)sleep調(diào)用阻塞,那么將會(huì)拋出interrupedException異常。
static boolean interrupted():測(cè)試當(dāng)前線程(當(dāng)前正在執(zhí)行命令的這個(gè)線程)是否被中斷。注意這是個(gè)靜態(tài)方法,調(diào)用這個(gè)方法會(huì)產(chǎn)生一個(gè)副作用那就是它會(huì)將當(dāng)前線程的中斷狀態(tài)重置為false。
boolean isInterrupted():判斷線程是否被中斷,這個(gè)方法的調(diào)用不會(huì)產(chǎn)生副作用即不改變線程的當(dāng)前中斷狀態(tài)。
static Thread currentThread() : 返回代表當(dāng)前執(zhí)行線程的Thread對(duì)象。
守護(hù)進(jìn)程
用來服務(wù)于不是服務(wù)進(jìn)程的其他所有當(dāng)前進(jìn)程下的所有線程
實(shí)現(xiàn)deamon.setDaemon(true)就行,要在線程開啟之前啟用
舉例
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();// 啟動(dòng)線程 try { Thread.sleep(1000); } catch (InterruptedException e) { // TODO Auto-generated catch block e.printStackTrace(); } Thread deamon = new Thread(new DaemonRunner(), "DaemonRunner"); // 設(shè)置為守護(hù)線程 deamon.setDaemon(true); deamon.start();// 啟動(dòng)線程 } 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("進(jìn)入守護(hù)線程,說明現(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í)行結(jié)果:
Thread[DaemonRunner,5,main]
進(jìn)入守護(hù)線程,說明現(xiàn)在還有其他線程在執(zhí)行
我是其他線程
首先,先啟動(dòng)其他線程,需要耗時(shí)1500ms,同時(shí),主線程耗時(shí)1000ms后,開始進(jìn)入守護(hù)線程,此時(shí)其它線程還在運(yùn)行,到了守護(hù)線程,耗時(shí)300ms,其他線程仍在執(zhí)行,繼續(xù)往下,守護(hù)線程執(zhí)行完畢
但是如果我把守護(hù)線程的300ms改成500ms,會(huì)發(fā)生什么事呢?
出現(xiàn)過兩種情況,畢竟在臨界值
1.我是其他線程
2.Thread[DaemonRunner,5,main]
進(jìn)入守護(hù)線程,說明現(xiàn)在還有其他線程在執(zhí)行
我是其他線程
相關(guān)文章
Java8中新特性O(shè)ptional、接口中默認(rèn)方法和靜態(tài)方法詳解
Java 8 已經(jīng)發(fā)布很久了,很多報(bào)道表明Java 8 是一次重大的版本升級(jí)。下面這篇文章主要給大家介紹了關(guān)于Java8中新特性O(shè)ptional、接口中默認(rèn)方法和靜態(tài)方法的相關(guān)資料,文中通過示例代碼介紹的非常詳細(xì),需要的朋友可以參考下。2017-12-12elasticsearch kibana簡(jiǎn)單查詢講解
今天小編就為大家分享一篇關(guān)于elasticsearch kibana簡(jiǎn)單查詢講解,小編覺得內(nèi)容挺不錯(cuò)的,現(xiàn)在分享給大家,具有很好的參考價(jià)值,需要的朋友一起跟隨小編來看看吧2019-02-02Netty分布式Future與Promise執(zhí)行回調(diào)相關(guān)邏輯剖析
這篇文章主要為大家介紹了Netty分布式Future與Promise執(zhí)行回調(diào)相關(guān)邏輯剖析,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步2022-03-03Java多線程連續(xù)打印abc實(shí)現(xiàn)方法詳解
這篇文章主要介紹了Java多線程連續(xù)打印abc實(shí)現(xiàn)方法詳解,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2020-03-03