Java多線程Thread基礎學習
1. 創(chuàng)建線程
1.1 通過構造函數: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 直接實現Runnable接口:
示例:
Thread thread2 = new Thread(new Runnable{}{ public void run(){ System.out.println("This is my thread."); } });
2. 運行線程
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. 停止線程,
千萬不用stop(),stop會立即終止線程。
通過interrupt()中斷線程,但是中斷并沒有停止線程,配合異常來實現:
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的話,可能會導致interrupt被捕獲,而繞過if(this.interrupted())的判斷而無法終止線程。
6. 等待和通知
線程等待:當前線程就處于等待狀態(tài),直到其他線程調用了notify()方法,線程才會繼續(xù)執(zhí)行
public final void wait() throws InterruptedException
線程通知:
public final native void notify()
注意:在notify()方法后,當前線程不會馬上釋放該對象鎖,要等到執(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(); } }
輸出結果:
7. 線程優(yōu)先級
高優(yōu)先級的線程將會獲得更多的CPU資源。一共分為10個優(yōu)先級。
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;
可見線程最高優(yōu)先級為10, 最低為1, 默認為5.
當設定的newPriority高于該線程組ThreadGroup的最高Priority時,只能分配該線程組的最高Priority
8. 守護線程
類似守護進程,Java存在兩種線程:用戶線程和守護線程。它是一種特殊線程,執(zhí)行的是一種后臺服務,當一個系統中不存在非守護線程的時候,守護線程會自己銷毀。典型的守護線程: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);//設置守護線程 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(); } } } } }
源碼分析:
設置線程為用戶線程(user thread)或守護線程(daemon thread),當剩余運行的線程均為守護線程時,JVM會退出。
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); } }
該方法用于判斷當前運行的線程是否有修改此線程的權限。
而public final native boolean isAlive();用于判斷該線程是否處于alive狀態(tài),即該線程是否已經start,且沒有die。
當isAlive的話就會拋出IllegalThreadStateException異常。
所以,設置守護線程的方法,邏輯就是先判斷當前線程是否有修改的權限,再判斷是否處于alive狀態(tài),如果不處于alive狀態(tài),則根據boolean變量on的值更改它的狀態(tài),即true:設為daemon線程,false:設為user線程。
到此這篇關于Java多線程基礎學習的文章就介紹到這了,更多相關Java多線程基礎內容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關文章希望大家以后多多支持腳本之家!