日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程语言 > java >内容正文

java

JavaSE入门学习51:多线程编程(二)

發布時間:2025/3/21 java 21 豆豆
生活随笔 收集整理的這篇文章主要介紹了 JavaSE入门学习51:多线程编程(二) 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

二Java多線程

? ? ? ?(4)線程的狀態

? ? ? ?線程狀態轉換示意圖:


? ? ? ?線程控制的基本方法:


? ? ? ?1)sleep()方法

? ? ? ?可以調用Thread的靜態方法:

? ? ? ?public static void sleep(long millis) throws interruptedException

? ? ? ?使得當前線程休眠(暫時停止執行millis毫秒)

? ? ? ?由于是靜態方法,sleep()方法可以由類名直接調用

? ? ? ?thread.sleep(...)

? ? ? ?實例:

? ? ? ?打印十次當前日期時間

[java]?view plaincopy print?
  • <span?style="font-size:18px;">import?java.util.*;??
  • ??
  • public?class?TestInterrupt{??
  • ????public?static?void?main(String[]?args){??
  • ????????MyThread?thread?=?new?MyThread();??
  • ????????????????thread.start();??
  • ????????????????try{??
  • ????????????Thread.sleep(10000);??
  • ????????}catch(InterruptedException?e){}??
  • ????????????????//中斷線程,不建議使用stop()方法??
  • ????????????????thread.interrupt();??
  • ????????}??
  • }??
  • ??
  • class?MyThread?extends?Thread{??
  • ????boolean?flag?=?true;??
  • ??????
  • ????public?void?run(){??
  • ????????while(flag){??
  • ????????System.out.println("==="+new?Date()+"===");??
  • ?????????????????try{??
  • ?????????????sleep(1000);??
  • ?????????????????}catch(InterruptedException?e){??
  • ?????????????return;??
  • ?????????????????}??
  • ????????????}??
  • ????}??
  • }</span>??
  • ? ? ? ?運行結果:


    ? ? ? ?打印兩次間隔5秒的目前時間然后結束

    [java]?view plaincopy print?
  • <span?style="font-size:18px;">import?java.util.*;??
  • ??
  • public?class?TestInterrupt{??
  • ????public?static?void?main(String[]?args){??
  • ????????MyThread?thread?=?new?MyThread();??
  • ????????????????thread.start();??
  • ????????????????try{??
  • ????????????Thread.sleep(10000);??
  • ????????}catch(InterruptedException?e){}??
  • ????????//中斷線程,不建議使用stop()方法??
  • ????????????????thread.interrupt();??
  • ???????}??
  • }??
  • ??
  • class?MyThread?extends?Thread{??
  • ????boolean?flag?=?true;??
  • ??????
  • ???????public?void?run(){??
  • ????????while?(flag){??
  • ????????????String?temp?=?new?Date().toString();??
  • ????????????????????????String?t?=?temp.substring(11,?temp.indexOf('C'));??
  • ????????????????????????t?=?t.trim();??
  • ????????????????????????String[]?time?=?t.split(":");??
  • ????????????????????????if(time.length?==?3){??
  • ????????????????System.out.println("現在是:"?+?time[0]?+?"點"?+?time[1]?+?"分"?+?time[2]?+?"秒");??
  • ????????????????????????}??
  • ???????????????????????try{??
  • ????????????????Thread.sleep(5000);??
  • ???????????????????????}catch(InterruptedException?e){??
  • ????????????????return;??
  • ??????????????????????}????
  • ??????????????}??
  • ??????}??
  • }</span>??
  • ? ? ? ?運行結果


    ? ? ? ?2)join()方法

    ? ? ? ?join()方法用于合并某個線程,也就是等著線程1執行完畢后,主線程才會執行。

    [java]?view plaincopy print?
  • <span?style="font-size:18px;">public?class?TestJoin?{??
  • ????public?static?void?main(String[]?args){??
  • ????????MyThread2?t1?=?new?MyThread2("線程1");??
  • ????????????????t1.start();??
  • ????????????????try{??
  • ????????????t1.join();??
  • ????????????????}catch(InterruptedException?e)?{}??
  • ??????????
  • ????????????????for(int?i=1;i<=10;i++){??
  • ????????????????????System.out.println("主線程");??
  • ???????????????}??
  • ????}??
  • }??
  • ??
  • class?MyThread2?extends?Thread?{??
  • ?????MyThread2(String?s){??
  • ????????super(s);??
  • ?????}??
  • ????
  • ????public?void?run(){??
  • ????????????for(int?i?=1;i<=10;i++){??
  • ?????????System.out.println("我是:"+getName());??
  • ?????????????????try{??
  • ????????????sleep(1000);??
  • ??????????????????}catch(InterruptedException?e)?{??
  • ????????????????????????return;??
  • ?????????????????}??
  • ????????????}??
  • ????}??
  • }</span>??
  • ? ? ? ?運行結果:


    ? ? ? ?3)yield()方法

    ? ? ? ?讓出CPU,給其它線程執行的機會

    [java]?view plaincopy print?
  • <span?style="font-size:18px;">public?class?TestYield?{??
  • ????public?static?void?main(String[]?args)?{??
  • ????????MyThread3?t1?=?new?MyThread3("線程1");??
  • ????????????????MyThread3?t2?=?new?MyThread3("線程2");??
  • ????????????????t1.start();???
  • ????????t2.start();??
  • ????????}??
  • }??
  • ??
  • ??
  • class?MyThread3?extends?Thread?{??
  • ????MyThread3(String?s){??
  • ????????super(s);??
  • ????}??
  • ??????
  • ????????public?void?run(){??
  • ????????for(int?i?=1;i<=20;i++){??
  • ????????????System.out.println(getName()+":?"+i);??
  • ????????????????????????if(i%10==0){??
  • ??????????????????????????????yield();??
  • ????????????????????????}??
  • ????????????????}??
  • ???????}??
  • }</span>??
  • ? ? ? ?運行結果:


    ? ? ? ??(5)線程的優先級別

    ? ? ? ??Java提供一個線程調度器來監控程序中啟動后進入就緒狀態的所有線程。線程調度器按照線程的優先級決定應調

    度哪個線程來執行。

    ? ? ? ?線程的優先級用數字表示,范圍從1到10,一個線程的缺省優先級是5。

    ? ? ? ?Thread.MIN_PRIORITY=1

    ? ? ? ?Thread.MAX_PRIORITY=10

    ? ? ? ?Thread.NORM_PRIORITY=5

    ? ? ? ?還使用下述方法獲得或設置線程對象的優先級

    ? ? ? ?獲得線程對象的優先級的方法:

    ? ? ? ?int getPriority();

    ? ? ? ?設置線程對象的優先級的方法:

    ? ? ? ?void setPriority(int newPriority);

    ? ? ? ?實例1代碼:

    [java]?view plaincopy print?
  • <span?style="font-size:18px;">public?class?TestPriority?{??
  • ????public?static?void?main(String[]?args)?{??
  • ????????Thread?t1?=?new?Thread(new?T1());??
  • ????????Thread?t2?=?new?Thread(new?T2());??
  • ????????//設置線程優先級??
  • ????????t1.setPriority(Thread.NORM_PRIORITY?+?3);??
  • ????????t1.start();??
  • ????????t2.start();??
  • ????}??
  • }??
  • ??
  • class?T1?implements?Runnable?{??
  • ????public?void?run()?{??
  • ????????for(int?i=0;?i<100;?i++)?{??
  • ????????????System.out.println("線程1:?"?+?i);??
  • ????????}??
  • ????}??
  • }??
  • ??
  • class?T2?implements?Runnable?{??
  • ????public?void?run()?{??
  • ????????for(int?i=0;?i<100;?i++)?{??
  • ????????????System.out.println("線程2:?"?+?i);??
  • ????????}??
  • ????}??
  • }</span>??
  • ? ? ? ?運行結果:


    ? ? ? ? 線程1的優先級高于線程2,因此線程1獲得CPU的時間片多于線程2的時間片。

    ? ? ? ? 實例2代碼:

    [java]?view plaincopy print?
  • <span?style="font-size:18px;">public?class?TestThread6?{????
  • ????public?static?void?main(String?args[]){??
  • ????????Thread?t?=?new?Runner6();??
  • ????????????t.start();??
  • ??????????
  • ????????for(int?i=0;?i<10;?i++)?{??
  • ????????????System.out.println("MainThread主線程:?"?+?i);??
  • ????????}??
  • ????????}??
  • }??
  • ??
  • class?Runner6?extends?Thread?{??
  • ????public?void?run()?{??
  • ????????System.out.println(Thread.currentThread().isAlive());??
  • ????????for(int?i=0;i<10;i++)?{??
  • ????????????System.out.println("子線程?"?+?i);??
  • ????????}??
  • ????}??
  • }</span>??
  • ? ? ? ? 運行結果:


    ? ? ? ? Thread.currentThread().isAlive()方法用于測試線程是否處于活動狀態。

    ? ? ? ??三線程同步

    ? ? ? ? 在Java語言中,引入了對象互斥鎖的概念,保證共享數據操作的完整性,每個對象都對應于一個可稱為互斥鎖的

    標記,這個標記保證在任一時刻,只能有一個線程訪問的該對象。

    ? ? ? ??(1)synchronized關鍵字的用法

    ? ? ? ? 關鍵字synchronized來與對象的互斥鎖聯系,當某個對象synchronized修飾時,表明該對象在任一時刻只能有一

    個線程訪問。

    ? ? ? ? synchronized關鍵字的使用方法:

    [java]?view plaincopy print?
  • <span?style="font-size:18px;">synchronized(this){??
  • ????????num?++;??
  • ????????????try{??
  • ????????????????hread.sleep(1);??
  • ????????}catch(InterruptedException?e)?{}??
  • ????????????System.out.println(name+",?你是第"+num+"個使用timer的線程");??
  • }</span>??
  • ? ? ? ? synchronized關鍵字還可以放在方法聲明中,表示整個方法為同步方法,例如:

    ? ? ? ? public synchronized void add(String name){...}

    ? ? ? ??實例代碼:

    [java]?view plaincopy print?
  • <span?style="font-size:18px;">public?class?TestSync?implements?Runnable?{??
  • ????Timer?timer?=?new?Timer();??
  • ??????
  • ????public?void?run(){??
  • ????????timer.add(Thread.currentThread().getName());??
  • ????}??
  • ??????
  • ????public?static?void?main(String[]?args)?{??
  • ????????TestSync?test?=?new?TestSync();??
  • ????????Thread?t1?=?new?Thread(test);??
  • ????????Thread?t2?=?new?Thread(test);??
  • ????????t1.setName("t1");???
  • ????????t2.setName("t2");??
  • ????????t1.start();???
  • ????????t2.start();??
  • ????}??
  • }??
  • ??
  • class?Timer{??
  • ????private?static?int?num?=?0;??
  • ??
  • ????public?void?add(String?name){??
  • ????????num?++;??
  • ????????try?{Thread.sleep(1);}???
  • ????????catch?(InterruptedException?e)?{}??
  • ????????System.out.println(name+",?你是第"+num+"個使用timer的線程");??
  • ????}??
  • }</span>??
  • ? ? ? ? 運行結果:

    ? ? ? ?顯然上述的結果令我們不滿意,這就需要使用線程同步機制。? ? ?

    ? ? ? ?改寫后的使用同步鎖塊的代碼:

    [java]?view plaincopy print?
  • <span?style="font-size:18px;">public?class?TestSync?implements?Runnable?{??
  • ????Timer?timer?=?new?Timer();??
  • ??????
  • ????public?void?run(){??
  • ????????timer.add(Thread.currentThread().getName());??
  • ????}??
  • ??????
  • ????public?static?void?main(String[]?args)?{??
  • ????TestSync?test?=?new?TestSync();??
  • ????????Thread?t1?=?new?Thread(test);??
  • ????????Thread?t2?=?new?Thread(test);??
  • ????????t1.setName("t1");???
  • ????????t2.setName("t2");??
  • ????????t1.start();???
  • ????????t2.start();??
  • ????}??
  • }??
  • ??
  • ??
  • class?Timer{??
  • ????private?static?int?num?=?0;??
  • ??????
  • ????????public?void?add(String?name){??
  • ????????synchronized(this){??
  • ????????????num?++;??
  • ????????????????????try{??
  • ????????????????Thread.sleep(1);??
  • ????????????????}catch(InterruptedException?e)?{}??
  • ???????????????????System.out.println(name+",?你是第"+num+"個使用timer的線程");??
  • ????????????}??
  • ????????}??
  • }</span>??
  • ? ? ? ?運行結果:

    ? ? ? ?改寫后的使用同步鎖的代碼:

    [java]?view plaincopy print?
  • <span?style="font-size:18px;">public?class?TestSync?implements?Runnable?{??
  • ????Timer?timer?=?new?Timer();??
  • ??????
  • ????public?void?run(){??
  • ????????timer.add(Thread.currentThread().getName());??
  • ????????}??
  • ??????
  • ???????public?static?void?main(String[]?args)?{??
  • ????????TestSync?test?=?new?TestSync();??
  • ????????????????Thread?t1?=?new?Thread(test);??
  • ????????????????Thread?t2?=?new?Thread(test);??
  • ????????????????t1.setName("t1");???
  • ????????????????t2.setName("t2");??
  • ????????????????t1.start();???
  • ????????????????t2.start();??
  • ???????}??
  • }??
  • ??
  • class?Timer{??
  • ????private?static?int?num?=?0;??
  • ??????
  • ????????public?synchronized?void?add(String?name){??
  • ????????num?++;??
  • ????????????try{??
  • ????????????Thread.sleep(1);??
  • ????????}catch(InterruptedException?e)?{}??
  • ???????????System.out.println(name+",?你是第"+num+"個使用timer的線程");??
  • ????}??
  • }</span>??
  • ? ? ? ?編譯運行是和上述同樣的結果。

    ? ? ? ?(2)死鎖

    ? ? ? ?所謂死鎖: 是指兩個或兩個以上的進程在執行過程中,由于競爭資源或者由于彼此通信而造成的一種阻塞的現

    象,若無外力作用,它們都將無法推進下去。此時稱系統處于死鎖狀態或系統產生了死鎖,這些永遠在互相等待的進

    程稱為死鎖進程。

    ? ? ? ?死鎖產生的實例代碼:

    [java]?view plaincopy print?
  • <span?style="font-size:18px;">public?class?TestDeadLock?implements?Runnable?{??
  • ????public?int?flag?=?1;??
  • ????static?Object?o1?=?new?Object(),?o2?=?new?Object();??
  • ??????
  • ????public?void?run()?{??
  • ????????System.out.println("flag="?+?flag);??
  • ??????????
  • ????????if(flag?==?1)?{??
  • ????????????synchronized(o1)?{??
  • ????????????????try?{??
  • ????????????????????Thread.sleep(500);??
  • ????????????????}?catch?(Exception?e)?{??
  • ????????????????????e.printStackTrace();??
  • ????????????????}??
  • ????????????????synchronized(o2)?{??
  • ????????????????????System.out.println("1");??????
  • ????????????????}??
  • ????????????}??
  • ????????}??
  • ??????????
  • ????????if(flag?==?0)?{??
  • ????????????synchronized(o2)?{??
  • ????????????????try?{??
  • ????????????????????Thread.sleep(500);??
  • ????????????????}?catch?(Exception?e)?{??
  • ????????????????????e.printStackTrace();??
  • ????????????????}??
  • ????????????????synchronized(o1)?{??
  • ????????????????????System.out.println("0");??
  • ????????????????}??
  • ????????????}??
  • ????????}??
  • ????}?????
  • ??????
  • ????public?static?void?main(String[]?args)?{??
  • ????????TestDeadLock?td1?=?new?TestDeadLock();??
  • ????????TestDeadLock?td2?=?new?TestDeadLock();??
  • ????????td1.flag?=?1;??
  • ????????td2.flag?=?0;??
  • ????????Thread?t1?=?new?Thread(td1);??
  • ????????Thread?t2?=?new?Thread(td2);??
  • ????????t1.start();??
  • ????????t2.start();??
  • ????}??
  • }</span>??
  • ? ? ? ?運行結果:

    X


    from:?http://blog.csdn.net/erlian1992/article/details/51707195

    總結

    以上是生活随笔為你收集整理的JavaSE入门学习51:多线程编程(二)的全部內容,希望文章能夠幫你解決所遇到的問題。

    如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。