相信在學習JAVA時大家估計對線程都有很深的印象吧,如今當我開始接觸Android開發時,真真正正的發現了線程是多麼的重要,現在就把我對線程的理解分享給大家。
大家一定要分清線程和進程不是一回事,進程是什么呢?進程就如我們需要執行class文件,而線程才是真正調用CPU資源來運行的。一個class文件一般只有一個進程,但線程可以有很多個,線程的執行時一種異步的執行方式。
不再扯這些沒用的了,直奔主題哈。
一、如何在main函數中再開啟一個線程:
public class Thread_one { public static void main(String [] args){ Run run = new Run(); //run.run();//此為方法的調用,和線程有著天壤之別 Thread thread = new Thread(run); thread.start();//啟動線程,調用線程的run()方法 for(int i=1; i<=20; i++){ System.out.二、線程中的sleep方法
public class Thread_sleep { /* * 線程停頓 */ public static void main(String [] args){ Runone run = new Runone(); Thread thread = new Thread(run); thread.start(); try { Thread.sleep(5000); thread.interrupt();//中斷線程的執行 //thread.stop();//相對中斷線程,stop過于粗暴,不建議使用,一般在需要強制關閉子線程時方使用此方法 } catch (InterruptedException e) { e.printStackTrace(); } }}class Runone implements Runnable{ @Override public void run() { for(int i=1 ; i<10; i++){ try { Thread.sleep(1000); System.out.println("-----"+new Date()+"-----"); } catch (InterruptedException e) { return ;//當捕獲到子線程被中斷時,直接關閉子線程 } } }}在這里特別說明一點,thread.interrupt();可以中斷線程的執行,相對stop溫柔那么一點點,不過還不是最佳的關閉線程的方法,下面就為大家提供一種方式:
public class Thread_stop { public static void main(String [] args){ Runthree run = new Runthree(); Thread th = new Thread(run); th.start(); try { Thread.sleep(5000); } catch (InterruptedException e) { e.printStackTrace(); } run.setStop(); }}class Runthree implements Runnable{ boolean flag; @Override public void run() { flag = true; int i = 0; while(flag){ try { System.out.println("子線程----"+(i++)); Thread.sleep(1000); } catch (InterruptedException e) { e.printStackTrace(); } } } public void setStop(){ flag = false; } }下面就簡單給大家介紹一下線程中的合并和讓出:
一、如何合并線程,此處調用了join()方法
public class Thread_join { /* * 合并線程 */ public static void main(String [] args){ Runtwo run = new Runtwo(); Thread thread = new Thread(run); thread.start(); try { thread.join();//合并線程,此時相當于方法調用 } catch (InterruptedException e) { e.printStackTrace(); } for(int i=0; i<10; i++){ System.out.println("主線程:"+i); } }}class Runtwo implements Runnable{ @Override public void run() { for(int i=0; i<10; i++){ System.out.println("子線程:----"+i); } } }二、如何讓出線程,此處調用了Thread的yield()方法:
public class Thread_yield { /**讓出CPU * @param args */ public static void main(String[] args) { Th th = new Th("aaa"); th.start(); for(int i = 0 ; i<=10; i++){ System.out.println("主線程----"+i); } }}class Th extends Thread{ Th(){} Th(String s){super(s);} @Override public void run() { for(int i = 0; i<=10; i++){ if(i%3!=0){ System.out.println("子線程"+i); }else{ System.out.println("子線程i="+i+" 線程進行切換"); yield();//從Thread繼承方可使用此方法 } } }}最后和大家分享一下關于線程的優先級的問題:
public class Thread_priority { /* * priority設置線程的優先級 * Thread默認的優先級為5;Thread的最大優先級為10,最小為0 */ public static void main(String [] args){ T1 t1 = new T1(); T2 t2 = new T2(); t1.start(); //t1.setPriority(Thread.NORM_PRIORITY+3);//設置t1的優先級 t2.start(); }}class T1 extends Thread{ @Override public void run() { for(int i = 0; i<50; i++){ System.out.println("線程T1-----"+i); } }}class T2 extends Thread{ @Override public void run() { for(int i = 0; i<50; i++){ System.out.println("線程T2"+i); } } }相信大家通過以上代碼基本已經了解JAVA中的線程機制,對于線程的同步,我會在下一篇文章中和大家進行交流,如有什么疑問歡迎騷擾。
最后聲明一下,以上內容如有錯誤,還望指點。
新聞熱點
疑難解答