Java多線程Thread基礎(chǔ)學(xué)習(xí)
1. 創(chuàng)建線程
1.1 通過(guò)構(gòu)造函數(shù):public Thread(Runnable target, String name){} 或:public Thread(Runnable target){}
示例:
Thread thread1 = new Thread(new MyThread(), "mythread"); class MyThread extends Thread(){ public void run(){ System.out.println("My First Thread'); } }
1.2 直接實(shí)現(xiàn)Runnable接口:
示例:
Thread thread2 = new Thread(new Runnable{}{ public void run(){ System.out.println("This is my thread."); } });
2. 運(yùn)行線程
thead1.start()
3. sleep
try{ #休眠1000ms Thread.sleep(1000); }catch(InterruptedException e){ e.printStackTrace(); }
4. getName() 獲取線程名字, getId()獲取線程id
System.out.println(Thread.currentThread().getName() + ":"+ Thread.currentThread().getId);
5. 停止線程,
千萬(wàn)不用stop(),stop會(huì)立即終止線程。
通過(guò)interrupt()中斷線程,但是中斷并沒(méi)有停止線程,配合異常來(lái)實(shí)現(xiàn):
public class Main { public static void main(String[] args) throws InterruptedException { try{ Thread thread1=new Thread(new TheThread(),"thread1"); thread1.start(); Thread.sleep(2000); thread1.interrupt(); }catch (InterruptedException e){ e.printStackTrace(); } } } class TheThread extends Thread{ public void run() { super.run(); for (int i = 0; i < 10; i++) { if(this.interrupted()){ break; } System.out.println(Thread.currentThread().getName() + ":" + i); } } }
注意,如果在TheThread類里加入catch InterruptException的話,可能會(huì)導(dǎo)致interrupt被捕獲,而繞過(guò)if(this.interrupted())的判斷而無(wú)法終止線程。
6. 等待和通知
線程等待:當(dāng)前線程就處于等待狀態(tài),直到其他線程調(diào)用了notify()方法,線程才會(huì)繼續(xù)執(zhí)行
public final void wait() throws InterruptedException
線程通知:
public final native void notify()
注意:在notify()方法后,當(dāng)前線程不會(huì)馬上釋放該對(duì)象鎖,要等到執(zhí)行notify()方法的線程將程序執(zhí)行完,也就是退出同步代碼塊中。
package wait.notify; public class ThreadWaitNotifyTest { final static Object object=new Object(); public static class T1 extends Thread{ public void run(){ System.out.println(System.currentTimeMillis()+": T1 start"); synchronized (object){ try { System.out.println(System.currentTimeMillis()+": T1 wait"); object.wait(); } catch (InterruptedException e) { e.printStackTrace(); } } System.out.println(System.currentTimeMillis()+": T1 end"); } } public static class T2 extends Thread{ public void run(){ System.out.println(System.currentTimeMillis()+": T2 start"); synchronized (object){ System.out.println("T2 synchonized code start."); object.notify(); try { Thread.sleep(2000); } catch (InterruptedException e) { e.printStackTrace(); }finally{ System.out.println("T2 synchonized code end."); } } try { Thread.sleep(2000); } catch (InterruptedException e) { e.printStackTrace(); } System.out.println(System.currentTimeMillis()+": T2 end"); } } public static void main(String[] args){ Thread thread1=new T1(); Thread thread2=new T2(); thread1.start(); thread2.start(); } }
輸出結(jié)果:
7. 線程優(yōu)先級(jí)
高優(yōu)先級(jí)的線程將會(huì)獲得更多的CPU資源。一共分為10個(gè)優(yōu)先級(jí)。
public final void setPriority(int newPriority)
源碼分析:
public final void setPriority(int newPriority) { ThreadGroup g; checkAccess(); if (newPriority > MAX_PRIORITY || newPriority < MIN_PRIORITY) { throw new IllegalArgumentException(); } if((g = getThreadGroup()) != null) { if (newPriority > g.getMaxPriority()) { newPriority = g.getMaxPriority(); } setPriority0(priority = newPriority); } }
public final static int MIN_PRIORITY = 1; public final static int NORM_PRIORITY = 5; public final static int MAX_PRIORITY = 10;
可見(jiàn)線程最高優(yōu)先級(jí)為10, 最低為1, 默認(rèn)為5.
當(dāng)設(shè)定的newPriority高于該線程組ThreadGroup的最高Priority時(shí),只能分配該線程組的最高Priority
8. 守護(hù)線程
類似守護(hù)進(jìn)程,Java存在兩種線程:用戶線程和守護(hù)線程。它是一種特殊線程,執(zhí)行的是一種后臺(tái)服務(wù),當(dāng)一個(gè)系統(tǒng)中不存在非守護(hù)線程的時(shí)候,守護(hù)線程會(huì)自己銷毀。典型的守護(hù)線程:JVM的垃圾回收線程。
public final void setDaemon(boolean on)
示例:
public class Main { public static void main(String[] args) throws InterruptedException { TheThread theThread=new TheThread(); theThread.setDaemon(true);//設(shè)置守護(hù)線程 theThread.start(); Thread.sleep(5000); System.out.println("全都退出啦"); } public static class TheThread extends Thread{ public void run(){ int i = 0; while (true){ i++; System.out.println(Thread.currentThread().getId()+":"+i); try { Thread.sleep(2000); } catch (InterruptedException e) { e.printStackTrace(); } } } } }
源碼分析:
設(shè)置線程為用戶線程(user thread)或守護(hù)線程(daemon thread),當(dāng)剩余運(yùn)行的線程均為守護(hù)線程時(shí),JVM會(huì)退出。
public final void setDaemon(boolean on) { checkAccess(); if (isAlive()) { throw new IllegalThreadStateException(); } daemon = on; }
其中checkAccesss()方法如下:
public final void checkAccess() { SecurityManager security = System.getSecurityManager(); if (security != null) { security.checkAccess(this); } }
該方法用于判斷當(dāng)前運(yùn)行的線程是否有修改此線程的權(quán)限。
而public final native boolean isAlive();用于判斷該線程是否處于alive狀態(tài),即該線程是否已經(jīng)start,且沒(méi)有die。
當(dāng)isAlive的話就會(huì)拋出IllegalThreadStateException異常。
所以,設(shè)置守護(hù)線程的方法,邏輯就是先判斷當(dāng)前線程是否有修改的權(quán)限,再判斷是否處于alive狀態(tài),如果不處于alive狀態(tài),則根據(jù)boolean變量on的值更改它的狀態(tài),即true:設(shè)為daemon線程,false:設(shè)為user線程。
到此這篇關(guān)于Java多線程基礎(chǔ)學(xué)習(xí)的文章就介紹到這了,更多相關(guān)Java多線程基礎(chǔ)內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
pagehelper插件顯示total為-1或1的問(wèn)題
這篇文章主要介紹了pagehelper插件顯示total為-1或1,本文給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2020-09-09Spring純注解開發(fā)模式讓開發(fā)簡(jiǎn)化更簡(jiǎn)化
Spring3.0引入了純注解開發(fā)的模式,框架的誕生是為了簡(jiǎn)化開發(fā),那注解開發(fā)就是簡(jiǎn)化再簡(jiǎn)化。Spring的特性在整合MyBatis方面體現(xiàn)的淋漓盡致哦2022-08-08