线程的构造和运行
① 用Thread類構造線程對象(繼承Thread類來創建并啟動多線程)
package cn.sxt.thread;/*** 創建線程方式一:* 1、創建:繼承Thread+重寫run* 2、啟動:創建子類對象+start* @author 1979**/public class StartThread extends Thread{/*** 線程入口點*/@Overridepublic void run() {for(int i=0;i<20;i++) {System.out.println("一邊聽歌");}}public static void main(String[] args) {/* 如果放在這里,那么要先敲完代碼再聽歌,for循環結束后才啟動線程* for(int i=0;i<20;i++) {* System.out.println("一邊coding");* }*///創建子類對象StartThread st = new StartThread();//啟動st.start();//不保證立即運行 由cpu調用//st.run();//普通方法調用,只能聽完歌再敲代碼for(int i=0;i<20;i++) {System.out.println("一邊coding");}} }【運行結果】
一邊coding 一邊coding ... 一邊聽歌 一邊聽歌 ... 一邊coding 一邊coding【缺點】:由于java采用單繼承機制,若為實現多線程而繼承了Thread,將無法再繼承其他類,是非常不明智的做法。
② 用Runnable輔助構造線程(實現Runnable接口創建線程類)
package cn.sxt.thread;/**** 創建線程方式二* 1、創建:實現Runnable+重寫run* 2、啟動:創建實現類對象+Thread對象+start* * 推薦:避免單繼承的局限性,優先使用接口* 方便共享資源* @author 1979**/ public class StartRun implements Runnable{/*** 線程入口點*/@Overridepublic void run() {for(int i=0;i<20;i++) {System.out.println("一邊聽歌");}}public static void main(String[] args) {/* 如果放在這里,那么要先敲完代碼再聽歌,for循環結束后才啟動線程* for(int i=0;i<20;i++) {* System.out.println("一邊coding");* }*///創建實現類對象StartRun sr = new StartRun();//創建代理類對象Thread t = new Thread(sr);//啟動t.start();//不保證立即運行 由cpu調用//st.run();//普通方法調用,只能聽完歌再敲代碼for(int i=0;i<20;i++) {System.out.println("一邊coding");}} }【運行結果】
一邊coding 一邊coding ... 一邊聽歌 一邊聽歌 ... 一邊coding 一邊coding package liti_07;/*本例展現這樣一種方式:利用Runnable接口直接構造線程并運行*有如下關鍵點:*1、希望該類中創建線程對象,就必須有線程成員,即私有變量t;*2、在構造函數中構造t引用的線程對象,注意把自己作為參數傳給t;*3、還需要啟動線程。由于t長設為私有,故不能直接t.start(),* 需要設置其他方法,如本例另提供一個公共的start()方法。**/ public class Ch_7_3 implements Runnable{public static void main (String[] args) {System.out.print("Main 開始");Ch_7_3 m1=new Ch_7_3(1,"奇數線程"); //注意,m1依舊不是線程對象Ch_7_3 m2=new Ch_7_3(2,"偶數線程");m1.start(); //注意,調用的是類R自己定義的start()m2.start();System.out.print("當前共有"+ Thread.activeCount()+"個線程");System.out.print("Main 結束");}private int d;private Thread t; //-----新增成員public void start(){ t.start(); } //-----關鍵點2 public Ch_7_3(int x, String s){d=x;//t=new Thread(this); t.setName(s); //----關鍵點1t=new Thread(this,s);}public void run(){for(int i=d; i<50;i=i+2)System.out.print(" "+i);//Thread t=Thread.currentThread(); //---此句不再需要System.out.print(t.getName()+"結束!");} }線程的一些常用方法
1、currentThread()
返回對當前正在執行的線程對象的引用。
2、getId()
返回此線程的標識符
3、getName()
返回此線程的名稱
4、getPriority()
返回此線程的優先級
5、isAlive()
測試這個線程是否還處于活動狀態。
什么是活動狀態呢?
活動狀態就是線程已經啟動且尚未終止。線程處于正在運行或準備運行的狀態。
6、sleep(long millis)
使當前正在執行的線程以指定的毫秒數“休眠”(暫時停止執行),具體取決于系統定時器和調度程序的精度和準確性。
7、interrupt()
中斷這個線程。
8、interrupted() 和isInterrupted()
interrupted():測試當前線程是否已經是中斷狀態,執行后具有將狀態標志清除為false的功能
isInterrupted(): 測試線程Thread對相關是否已經是中斷狀態,但不清楚狀態標志
9、 setName(String name)
將此線程的名稱更改為等于參數 name 。
10、isDaemon()
測試這個線程是否是守護線程。
11、setDaemon(boolean on)
將此線程標記為 daemon線程或用戶線程。
12、join()
在很多情況下,主線程生成并起動了子線程,如果子線程里要進行大量的耗時的運算,主線程往往將于子線程之前結束,但是如果主線程處理完其他的事務后,需要用到子線程的處理結果,也就是主線程需要等待子線程執行完成之后再結束,這個時候就要用到join()方法了。
join()的作用是:“等待該線程終止”,這里需要理解的就是該線程是指的主線程等待子線程的終止。也就是在子線程調用了join()方法后面的代碼,只有等到子線程結束了才能執行
13、yield()
yield()方法的作用是放棄當前的CPU資源,將它讓給其他的任務去占用CPU時間。注意:放棄的時間不確定,可能一會就會重新獲得CPU時間片。
14、setPriority(int newPriority)
更改此線程的優先級
創作挑戰賽新人創作獎勵來咯,堅持創作打卡瓜分現金大獎總結
- 上一篇: 数据结构与算法——二分查找与二叉排序树
- 下一篇: 数据结构与算法——二叉树与图