本篇將為大家總結JAVA中的線程機制,談到線程,大家一定會問線程和進程有什么區別?剛接觸進程時我也有這樣的疑問,今天就為大家簡單介紹一下進程和線程。進程(PRocess)是計算機中的程序關于某數據集合上的一次運行活動,是系統進行資源分配和調度的基本單位,是操作系統結構的基礎;線程是程序內部的順序控制流。它們的區別:
每個進程都有獨立的代碼和數據空間,進程間的切換會有較多的開銷;線程可以看做輕量級的進程,同以進程共享代碼塊和數據空間,每個進程有獨立的運行棧和程序計數器(PC),線程間切換開銷小。
JAVA的線程是通過java.lang.Thread類來實現的,VM啟動時會有一個由主方法(public static void main(){})所定義的線程。可以通過創建Thread的實例來創建新的線程。每個線程都是通過某個特定的Thread對象所對應的方法run()來完成其操作的,方法run()稱為線程體。通過調用Thread的start()的方法來啟動線程。
1、線程的創建與啟動:
public class Th { /** * @param 線程的創建與啟動 */ public static void main(String[] args) { //第一種線程創建方式 Runnable run = new myRunnable(); Thread th1 = new Thread(run); //第二種線程創建方式 myThread th2 = new myThread(); //線程啟動 th1.start(); th2.start(); for(int i=0; i<50; i++){ System.out.println("主線程"); } }}class myRunnable implements Runnable{ public void run() { for(int i=0; i<50; i++){ System.out.println("線程一"); } }}class myThread extends Thread{ public void run() { for(int i=0; i<50; i++){ System.out.println("線程二"); } }}2、線程的基本控制:

3、sleep、join、yield:
sleep(int time):休眠,當前線程休眠time毫秒
join():線程合并
yield:讓出CPU,給其它線程執行的機會
實例代碼:
public class SJY { /** * @param sleep、join、yield的使用 */ public static void main(String[] args) { Runnable run = new myRun(); Thread th1 = new Thread(run); myTh th2 = new myTh(); for(int i=0; i<50; i++){ if(i!=25){ System.out.println("主線程"); }else{ th1.yield();//讓出CPU } } th1.start(); th2.start(); try { th1.join();//線程合并 th1.sleep(1000*3);//休眠3秒 } catch (InterruptedException e) { e.printStackTrace(); } }}class myRun implements Runnable{ public void run() { for(int i=0; i<50; i++){ System.out.println("線程一"); } }}class myTh extends Thread{ public void run() { for(int i=0; i<50; i++){ System.out.println("線程二"); } }}4、線程優先級:
JAVA提供了一個線程調度器來監控程序中啟動后進入就緒狀態的所有線程,線程調度器根據線程的優先級決定應調那個線程來執行。線程的優先級用數字表示,范圍從1~10,一個線程的缺省優先級為5。Thread.MIN_PRIORITY=1;Thread.MAX_PRIORITY=10;Thread.NORM_PRIORITY=5。
使用下述方式獲得線程的優先級和設置線程的優先級:int getPriority():獲得線程的優先級,void setPriority(int newPriority):設置線程的優先級
5、線程同步:
線程同步是什么意思呢?這里加入了鎖的機制,synchronized是JAVA中加鎖的關鍵字。對于線程同步請看下面的實例:
public class SYN implements Runnable{ /** * @param 互斥鎖,線程同步 */ Text text = new Text(); public static void main(String[] args) { SYN syn = new SYN(); Thread th1 = new Thread(syn); Thread th2 = new Thread(syn); th1.setName("th1"); th2.setName("th2"); th1.start(); th2.start(); } public void run() { text.add(Thread.currentThread().getName()); }}class Text { private static int num=0; public synchronized void add(String name){ //synchronized (this) {//加鎖,保證線程同步 num++; try { Thread.sleep(1); System.out.println(name+",你是第"+num+"個使用Text的線程"); } catch (InterruptedException e) { e.printStackTrace(); } //} }}6、死鎖:
public class Test implements Runnable{ /** * @param 死鎖 */ public static void main(String [] args){ Test t1 = new Test(); Test t2 = new Test(); t1.flag=1; t2.flag=0; Thread th1 = new Thread(t1); Thread th2 = new Thread(t2); th1.start(); th2.start(); } private int flag = 1; static Object o1 = new Object(); static Object o2 = new Object(); public void run() { System.out.println("flag="+flag); if(flag == 1){ synchronized (o1) { try { Thread.sleep(5000); } catch (InterruptedException e) { e.printStackTrace(); } synchronized (o2) { System.out.println("1"); } } } if(flag == 0){ synchronized (o2) { try { Thread.sleep(5000); } catch (InterruptedException e) { e.printStackTrace(); } synchronized (o1) { System.out.println("2"); } } } }}到這里JAVA線程總結就完成了。下一篇網絡編程。
新聞熱點
疑難解答