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

歡迎訪問 生活随笔!

生活随笔

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

java

Java多线程之线程间协作 notify与wait的使用

發(fā)布時間:2025/4/5 java 54 豆豆
生活随笔 收集整理的這篇文章主要介紹了 Java多线程之线程间协作 notify与wait的使用 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

(轉載請注明出處:http://blog.csdn.net/buptgshengod

1.背景

?????? Java多線程操作運用很廣,特別是在android程序方面。線程異步協作是多線程操作的難點也是關鍵,也是找工作面試經常考到的地方。下面分享一下我的使用心得。

介紹幾個關鍵字

synchronized:線程鎖,使得系統(tǒng)只執(zhí)行當前線程。

notifyAll():喚醒其它被鎖住的線程

wait():掛起線程

ExecutorService exec=Executors.newCachedThreadPool();:創(chuàng)建線程池

exec.execute( new Runnable() ):將線程放到線程池中管理

2.代碼部分

(1)

public class Thread2 {public void m4t1() {synchronized(this) {int i = 5;while(i>0) {System.out.println("1");i--;try {Thread.sleep(500);} catch (InterruptedException ie) {}}}}public void m4t2() {// synchronized(this){int i = 5;while( i > 0) {System.out.println("2");i--;try {Thread.sleep(500);} catch (InterruptedException ie) {}}}public static void main(String[] args) {final Thread2 myt2 = new Thread2();Thread t1 = new Thread( new Runnable() { public void run() { myt2.m4t1(); } }, "t1" );Thread t2 = new Thread( new Runnable() { public void run() { myt2.m4t2(); } }, "t2" );t1.start();t2.start();} }
我們發(fā)現,兩個線程其中一個上了鎖,另一個沒有上鎖。運行結果如下。說明了雖然一個線程上了鎖,但是還是能被讓其它未上鎖線程訪問。

(2)

當我們給兩個方法都加上鎖

public class Thread2 {public void m4t1() {synchronized(this) {int i = 5;while(i>0) {System.out.println("1");i--;try {Thread.sleep(500);} catch (InterruptedException ie) {}}}}public void m4t2() {synchronized(this){int i = 5;while( i > 0) {System.out.println("2");i--;try {Thread.sleep(500);} catch (InterruptedException ie) {}}}}public static void main(String[] args) {final Thread2 myt2 = new Thread2();Thread t1 = new Thread( new Runnable() { public void run() { myt2.m4t1(); } }, "t1" );Thread t2 = new Thread( new Runnable() { public void run() { myt2.m4t2(); } }, "t2" );t1.start();t2.start();} } 運行結果變成了

先執(zhí)行完第一個鎖中的內容后執(zhí)行第二個鎖中的內容。


(3)

這時候我們使用notifyAll()和wait()

import java.util.concurrent.ExecutorService; import java.util.concurrent.Executors;public class Thread2 {public void m4t1() {synchronized(this) {int i = 5;while(i>0) {System.out.println("1");i--;try {Thread.sleep(500);notifyAll();wait();} catch (InterruptedException ie) {}}}}public void m4t2() {synchronized(this){int i = 5;while( i > 0) {System.out.println("2");i--;try {Thread.sleep(500);notifyAll();wait();} catch (InterruptedException ie) {}}}}public static void main(String[] args) {final Thread2 myt2 = new Thread2();Thread t1 = new Thread( new Runnable() { public void run() { myt2.m4t1(); } }, "t1" );Thread t2 = new Thread( new Runnable() { public void run() { myt2.m4t2(); } }, "t2" );t1.start();t2.start();} }
通過不斷的喚醒再掛起,兩個線程又交替運行。


(4)

如果想讓代碼更完善,可以將兩個線程放到線程池

import java.util.concurrent.ExecutorService; import java.util.concurrent.Executors;public class Thread2 {public void m4t1() {synchronized(this) {int i = 5;while(i>0) {System.out.println("1");i--;try {Thread.sleep(500);notifyAll();wait();} catch (InterruptedException ie) {}}}}public void m4t2() {synchronized(this){int i = 5;while( i > 0) {System.out.println("2");i--;try {Thread.sleep(500);notifyAll();wait();} catch (InterruptedException ie) {}}}}public static void main(String[] args) {final Thread2 myt2 = new Thread2();ExecutorService exec=Executors.newCachedThreadPool();exec.execute( new Runnable() { public void run() { myt2.m4t1(); } });exec.execute( new Runnable() { public void run() { myt2.m4t2(); } });} }

總結

以上是生活随笔為你收集整理的Java多线程之线程间协作 notify与wait的使用的全部內容,希望文章能夠幫你解決所遇到的問題。

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