Java多線程阻塞與喚醒代碼示例
java線程的阻塞及喚醒
1. sleep() 方法:
sleep(…毫秒),指定以毫秒為單位的時(shí)間,使線程在該時(shí)間內(nèi)進(jìn)入線程阻塞狀態(tài),期間得不到cpu的時(shí)間片,等到時(shí)間過去了,線程重新進(jìn)入可執(zhí)行狀態(tài)。(暫停線程,不會(huì)釋放鎖)
//測(cè)試sleep()方法
class Thread7 implements Runnable{
@Override
public void run() {
for(int i=0;i<50;i++){
System.out.println(Thread.currentThread().getName()+"num="+i);
try {
Thread.sleep(500);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}
class Thread8 implements Runnable{
@Override
public void run() {
for(int i=0;i<1000;i++){
System.out.println(Thread.currentThread().getName()+"num="+i);
}
}
}
public static void main(String[] args) {
/*
* 測(cè)試線程阻塞
*/
//測(cè)試sleep()方法
Thread7 t7=new Thread7();
Thread8 t8=new Thread8();
Thread t81=new Thread(t8, "餃子");
Thread t71=new Thread(t7, "包子");
Thread t72=new Thread(t7, "面包");
t71.start();
t81.start();
t72.start();
}
2.suspend() 和 resume() 方法:。
掛起和喚醒線程,suspend()使線程進(jìn)入阻塞狀態(tài),只有對(duì)應(yīng)的resume()被調(diào)用的時(shí)候,線程才會(huì)進(jìn)入可執(zhí)行狀態(tài)。(不建議用,容易發(fā)生死鎖)
//測(cè)試suspend()和resume()方法
class Thread9 implements Runnable{
@Override
public void run() {
for(long i=0;i<500000000;i++){
System.out.println(Thread.currentThread().getName()+" num= "+i);
}
}
}
public static void main(String[] args) {
//測(cè)試suspend和resume
Thread9 t9=new Thread9();
Thread t91=new Thread(t9,"包子");
t91.start();
try {
Thread.sleep(2000);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
t91.suspend();
try {
Thread.sleep(2000);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
t91.resume();
}
(在控制臺(tái)打印輸出的時(shí)候,會(huì)停頓2秒鐘,然后再繼續(xù)打印。)
3. yield() 方法:
會(huì)使的線程放棄當(dāng)前分得的cpu時(shí)間片,但此時(shí)線程任然處于可執(zhí)行狀態(tài),隨時(shí)可以再次分得cpu時(shí)間片。yield()方法只能使同優(yōu)先級(jí)的線程有執(zhí)行的機(jī)會(huì)。調(diào)用 yield() 的效果等價(jià)于調(diào)度程序認(rèn)為該線程已執(zhí)行了足夠的時(shí)間從而轉(zhuǎn)到另一個(gè)線程。(暫停當(dāng)前正在執(zhí)行的線程,并執(zhí)行其他線程,且讓出的時(shí)間不可知)
//測(cè)試yield()方法
class Thread10 implements Runnable{
@Override
public void run() {
for(int i=0;i<100;i++){
System.out.println(Thread.currentThread().getName()+" num= "+i);
if(i==33){
Thread.yield();
}
}
}
}
public static void main(String[] args) {
//測(cè)試yield
Thread10 t10 =new Thread10();
Thread t101=new Thread(t10, "包子");
Thread t102=new Thread(t10, "面包");
t101.start();
t102.start();
}
/*
運(yùn)行結(jié)果為:
……
包子 num= 24
包子 num= 25
包子 num= 26
包子 num= 27
包子 num= 28
包子 num= 29
包子 num= 30
包子 num= 31
包子 num= 32
包子 num= 33
面包 num= 0
面包 num= 1
面包 num= 2
面包 num= 3
……
面包 num= 30
面包 num= 31
面包 num= 32
面包 num= 33
包子 num= 34
包子 num= 35
包子 num= 36
包子 num= 37
包子 num= 38
……
*/
(可以看到,當(dāng)數(shù)字為33時(shí),都發(fā)生了交替。)
4.wait() 和 notify() 方法:
兩個(gè)方法搭配使用,wait()使線程進(jìn)入阻塞狀態(tài),調(diào)用notify()時(shí),線程進(jìn)入可執(zhí)行狀態(tài)。wait()內(nèi)可加或不加參數(shù),加參數(shù)時(shí)是以毫秒為單位,當(dāng)?shù)搅酥付〞r(shí)間或調(diào)用notify()方法時(shí),進(jìn)入可執(zhí)行狀態(tài)。(屬于Object類,而不屬于Thread類,wait( )會(huì)先釋放鎖住的對(duì)象,然后再執(zhí)行等待的動(dòng)作。由于wait( )所等待的對(duì)象必須先鎖住,因此,它只能用在同步化程序段或者同步化方法內(nèi),否則,會(huì)拋出異常IllegalMonitorStateException.)
//測(cè)試wait()和notify()方法
//用生產(chǎn)者和消費(fèi)者模式模擬這一過程
/*消費(fèi)者 */
class Consumer implements Runnable {
private Vector obj;
public Consumer(Vector v) {
this.obj = v;
}
public void run() {
synchronized (obj) {
while (true) {
try {
if (obj.size() == 0) {
obj.wait();
}
System.out.println("消費(fèi)者:我要買面包。");
System.out.println("面包數(shù): " + obj.size());
obj.clear();
obj.notify();
} catch (Exception e) {
e.printStackTrace();
}
}
}
}
}
/* 生產(chǎn)者 */
class Producter implements Runnable {
private Vector obj;
public Producter(Vector v) {
this.obj = v;
}
public void run() {
synchronized (obj) {
while (true) {
try {
if (obj.size() != 0) {
obj.wait();
}
obj.add(new String("面包"));
obj.notify();
System.out.println("生產(chǎn)者:面包做好了。");
Thread.sleep(500);
} catch (Exception e) {
e.printStackTrace();
}
}
}
}
}
public static void main(String[] args) {
//測(cè)試wait()和notify()
Vector obj = new Vector();
Thread consumer = new Thread(new Consumer(obj));
Thread producter = new Thread(new Producter(obj));
consumer.start();
producter.start();
}
5.join()方法
也叫線程加入。是當(dāng)前線程A調(diào)用另一個(gè)線程B的join()方法,當(dāng)前線程轉(zhuǎn)A入阻塞狀態(tài),直到線程B運(yùn)行結(jié)束,線程A才由阻塞狀態(tài)轉(zhuǎn)為可執(zhí)行狀態(tài)。
//測(cè)試join
class Thread11 implements Runnable{
@Override
public void run() {
System.out.println("Start Progress.");
try {
for(int i=0;i<5;i++){
System.out.println("Thread11線程 : "+i);
Thread.sleep(1000);
}
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
System.out.println("End Progress.");
}
}
public static void main(String[] args) {
//測(cè)試join
Thread11 t11=new Thread11();
Thread t111=new Thread(t11);
t111.start();
try {
t111.join();
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
System.out.println("hi,I'm Main線程");
}
/*
運(yùn)行結(jié)果為:
Start Progress.
Thread11線程 : 0
Thread11線程 : 1
Thread11線程 : 2
Thread11線程 : 3
Thread11線程 : 4
End Progress.
hi,I'm Main線程
*/
總結(jié)
本文關(guān)于Java多線程阻塞與喚醒代碼示例的介紹就到這里,希望對(duì)大家學(xué)習(xí)Java有所幫助。感興趣的朋友可以繼續(xù)參閱:Java多線程ForkJoinPool實(shí)例詳解 、 Java通過賣票理解多線程 、 Java線程安全基礎(chǔ)概念解析等。有什么問題可以隨時(shí)留言,或者有什么方面想要了解的,您也可以留言,小編會(huì)及時(shí)給您答復(fù)。希望大家對(duì)腳本之家多多支持!
相關(guān)文章
@Scheduled 如何讀取動(dòng)態(tài)配置文件
這篇文章主要介紹了@Scheduled 如何讀取動(dòng)態(tài)配置文件的操作,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2021-06-06
詳解WebSocket+spring示例demo(已使用sockJs庫(kù))
本篇文章主要介紹了WebSocket spring示例demo(已使用sockJs庫(kù)),小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧2017-01-01
Java?ArrayList實(shí)現(xiàn)班級(jí)信息管理系統(tǒng)
這篇文章主要為大家詳細(xì)介紹了Java?ArrayList實(shí)現(xiàn)班級(jí)信息管理系統(tǒng),文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2022-02-02
Java分支結(jié)構(gòu)程序設(shè)計(jì)實(shí)例詳解
這篇文章主要介紹了Java分支結(jié)構(gòu)程序設(shè)計(jì)例題,本文通過實(shí)例代碼給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2022-01-01
Spring lazy-init 懶加載的原理小結(jié)
lazy-init 是一個(gè)非常重要的屬性,可以優(yōu)化應(yīng)用的啟動(dòng)時(shí)間,尤其是在處理大量bean或者復(fù)雜依賴關(guān)系時(shí),可以顯著提高應(yīng)用的響應(yīng)速度,本文主要介紹了Spring lazy-init 懶加載的原理小結(jié),感興趣的可以了解一下2025-04-04
spring boot創(chuàng)建和數(shù)據(jù)庫(kù)關(guān)聯(lián)模塊詳解
這篇文章主要給大家介紹了關(guān)于spring boot創(chuàng)建和數(shù)據(jù)庫(kù)關(guān)聯(lián)模塊的相關(guān)資料,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2020-10-10
Spring用三級(jí)緩存處理循環(huán)依賴的方法詳解
這篇文章主要介紹了Spring用三級(jí)緩存處理循環(huán)依賴的方法,在Spring?框架中,依賴注入是其核心特性之一,它允許對(duì)象之間的依賴關(guān)系在運(yùn)行時(shí)動(dòng)態(tài)注入,然而,當(dāng)多個(gè)Bean之間的依賴關(guān)系形成一個(gè)閉環(huán)時(shí),就會(huì)出現(xiàn)循環(huán)依賴問題,本文就為解決此問題,需要的朋友可以參考下2025-02-02
淺談Java中對(duì)類的主動(dòng)引用和被動(dòng)引用
這篇文章主要介紹了淺談Java中對(duì)類的主動(dòng)引用和被動(dòng)引用,分享了相關(guān)代碼示例,小編覺得還是挺不錯(cuò)的,具有一定借鑒價(jià)值,需要的朋友可以參考下2018-02-02

