1.什么時(shí)候使用多線程編程
“只有客戶發(fā)展了,才有我們的生存與發(fā)展!”這是成都創(chuàng)新互聯(lián)的服務(wù)宗旨!把網(wǎng)站當(dāng)作互聯(lián)網(wǎng)產(chǎn)品,產(chǎn)品思維更注重全局思維、需求分析和迭代思維,在網(wǎng)站建設(shè)中就是為了建設(shè)一個(gè)不僅審美在線,而且實(shí)用性極高的網(wǎng)站。創(chuàng)新互聯(lián)對(duì)網(wǎng)站建設(shè)、網(wǎng)站制作、網(wǎng)站制作、網(wǎng)站開發(fā)、網(wǎng)頁(yè)設(shè)計(jì)、網(wǎng)站優(yōu)化、網(wǎng)絡(luò)推廣、探索永無(wú)止境。
一個(gè)任務(wù)在正常情況下是按順序執(zhí)行的,但是如果當(dāng)前任務(wù)里有多個(gè)相似進(jìn)程塊(例如for,while語(yǔ)句),我們就可以考慮把這些代碼塊抽出來(lái)并行運(yùn)行,無(wú)需阻塞
2.實(shí)現(xiàn)多線程的幾種方式
一種是繼承Thread類重寫run方法,另一種是實(shí)現(xiàn)Runnable接口重寫run方法
啟動(dòng)多線程很多情況下是為了處理并發(fā)進(jìn)程,此時(shí)對(duì)于部分實(shí)時(shí)性要求不是那么高的業(yè)務(wù)需求,我們還可以通過(guò)實(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)程
用來(lái)服務(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ù)線程,說(shuō)明現(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ù)線程,說(shuō)明現(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)過(guò)兩種情況,畢竟在臨界值
1.我是其他線程
2.Thread[DaemonRunner,5,main]
進(jìn)入守護(hù)線程,說(shuō)明現(xiàn)在還有其他線程在執(zhí)行
我是其他線程
本文題目:實(shí)例總結(jié)Java多線程編程的方法
鏈接地址:http://vcdvsql.cn/article22/pdsccc.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供響應(yīng)式網(wǎng)站、微信小程序、關(guān)鍵詞優(yōu)化、品牌網(wǎng)站制作、網(wǎng)站策劃、外貿(mào)網(wǎng)站建設(shè)
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請(qǐng)盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如需處理請(qǐng)聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來(lái)源: 創(chuàng)新互聯(lián)