Java多线程之线程间协作 notify与wait的使用
生活随笔
收集整理的這篇文章主要介紹了
Java多线程之线程间协作 notify与wait的使用
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
(轉載請注明出處:http://blog.csdn.net/buptgshengod)
1.背景
?????? Java多線程操作運用很廣,特別是在android程序方面。線程異步協作是多線程操作的難點也是關鍵,也是找工作面試經常考到的地方。下面分享一下我的使用心得。
介紹幾個關鍵字:
synchronized:線程鎖,使得系統只執行當前線程。
notifyAll():喚醒其它被鎖住的線程
wait():掛起線程
ExecutorService exec=Executors.newCachedThreadPool();:創建線程池
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();} }
我們發現,兩個線程其中一個上了鎖,另一個沒有上鎖。運行結果如下。說明了雖然一個線程上了鎖,但是還是能被讓其它未上鎖線程訪問。
(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();} } 運行結果變成了先執行完第一個鎖中的內容后執行第二個鎖中的內容。
(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的使用的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 【Java设计模式】装饰模式
- 下一篇: 【android-tips】adb 常用