Java多线程之线程间协作 notify与wait的使用
来源:互联网 发布:刷搜狗手机点击优化 编辑:程序博客网 时间:2024/06/11 09:12
(转载请注明出处: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(); } }); }}
2 1
- Java多线程之线程间协作 notify与wait的使用
- Java:使用wait()与notify()实现线程间协作
- Java:使用wait()与notify()实现线程间协作
- Java:使用wait()与notify()实现线程间协作
- Java:使用wait()与notify()实现线程间协作
- Java:使用wait()与notify()实现线程间协作
- Java:使用wait()与notify()实现线程间协作
- Java:使用wait()与notify()实现线程间协作
- Java:使用wait()与notify()实现线程间协作
- Java:使用wait()与notify()实现线程间协作
- Java:使用wait()与notify()实现线程间协作
- 线程间协作wait()与notify()
- JAVA线程间协作:wait.notify.notifyAll
- JAVA线程间协作:wait.notify.notifyAll
- Java 线程间的协作(wait/notify/sleep/yield/join)
- java多线程之wait,notify的使用
- Java并发之线程间协作Object的wait()、notify()、notifyAll()
- java中线程的协作sleep yield wait 和 notify
- IOS开发 - CoreData详解
- 钟摆效果动画效果
- 部署Office Web Apps Server并配置其与SharePoint 2013的集成
- Android设计:.9.png图片的制作方法
- android全局异常检测并处理
- Java多线程之线程间协作 notify与wait的使用
- uva - 11300 - Spreading the Wealth(数学推导,中位数)
- 3.5.8 理解GC日志
- 叉包舍-DIY之无线网络连接受限故障分析
- 一直没用过 new try 记录一下
- MVC+DAO设计模式下的设计流程
- svn merge和branch
- servlet以及jsp中编码设置
- Qt常用类