------- android培训、java培训、期待与您交流! ----------
- 线程间的通信
案例图
总结:
1.在存东西后,立即唤醒输出线程,让它来取
2.在取到东西后,立即唤醒输入线程,让它赶快存东西
线程间的通信:
其实就是多个线程操作同一资源,但是操作的动作不同
wait() notify() notifyAll()(Object类的方法)
都使用在同步中,所用需要用监视器(锁)的线程操作
所以要使用在同步中,因为只有同步才有锁
为什么这些方法都要定义在Object中?
只用同一个线程上的被等待线程,可以被同一个锁上的notify唤醒
也就是说:等待和唤醒必须是同一把锁
锁可以是任意对象,所以这些方法定义在object中
案例代码:
class Res{ //用于存储共享数据(共享资源) String name; String sex; boolean flag=false; } class Input implements Runnable{ private Res r; public Input(Res r) { this.r=r; } public void run(){ int x=0; r.flag=false; while(true) { synchronized (r) { if(r.flag)//判断flag==true,则让input的线程状态变为:wait() { try { r.wait(); } catch (Exception e) { System.out.println(e.toString()); } } //flag==false 执行input操作 if(x==0) { r.name="zhangsan"; r.sex="man"; } else { r.name="李四"; r.sex="女女---"; } x=(x+1)%2; r.flag=true; try { //将ouput(输出线程)状态唤醒 r.notify(); } catch (Exception e) { // TODO: handle exception } } } } } class Output implements Runnable{ private Res r; boolean flag=false; public Output(Res r) { this.r=r; } public void run(){ while(true) { synchronized (r) { if(r.flag) { System.out.println(r.name+"----"+r.sex); flag=false; try { //将input线程唤醒,存东西 r.notify();//将线程池中的线程唤醒 } catch (Exception e) { // TODO: handle exception } } //r.flag==false try {//把自己(输出线程)状态变为 wait() r.wait(); } catch (Exception e) { // TODO: handle exception } } } } } public class ThreadDemo { public static void main(String[] args) { Res res=new Res(); Input input=new Input(res); Output output=new Output(res); Thread thread=new Thread(input); Thread thread02=new Thread(output); thread.start(); thread02.start(); } }
-------------代码优化 start---------------------
class Res{ //用于存储共享数据(共享资源) private String name; private String sex; private boolean flag=false; public synchronized void set(String name,String sex) { if(flag) 把自己的状态变为:wait()跳到output执行 try{this.wait();}catch(Exception e){} this.name=name; this.sex=sex; flag=true; try{this.notify();}catch(Exception e){} } public synchronized void out() { if(flag) System.out.println(this.name+"---"+this.sex); flag=false; //将input唤醒 try{this.notify();}catch(Exception e){} //把自己的状态变为:wait()跳到input执行 try{this.wait();}catch(Exception e){} } } class Input implements Runnable{ private Res res; Input(Res res) { this.res=res; } public void run(){ int x=0; while(true) { if(x==0) res.set("zhangsan", "man"); else res.set("王丽", "女"); x=(x+1)%2; } } } class Output implements Runnable{ private Res r; boolean flag=false; public Output(Res r) { this.r=r; } public void run(){ while(true) { r.out(); } } } public class ThreadDemo { public static void main(String[] args) { Res res=new Res(); new Thread(new Input(res)).start(); new Thread(new Output(res)).start(); } }
-------------代码优化 end---------------------
相关推荐
扫描网络计算机——多线程扫描网络计算机——多线程扫描网络计算机——多线程
|--利用FinalHttp实现多线程断点续传 |--加密之MD5 |--动画Animation详解 |--动画之view左右抖动 |--动画之移动动画 |--动画之组合动画 |--动画之缩放动画ScaleAnimation |--反序列化对象 |--发送短信 读天气 调音量...
多线程是一种软件实现多个线程并发执行任务的技术。在iOS开发中,实现多线程的的方式有很多种,常用的有pthread、NSThread、GCD与NSOperation。在实际应用中,多线程技术也有着非常多的使用场景,比如异步的网络请求...
易语言实例模块——多线程模块.e
JAVA项目——多线程下载代码 使用Eclispe
易语言实例模块——多线程支持模块.ec
NULL 博文链接:https://lvwenwen.iteye.com/blog/1470696
操作系统课程设计——多线程同步演示程序,采用visual C++编写
JAVA面试题——多线程
Java——多线程编程技术.pdf
android——Handler与多线程应用范例
IOS应用源码——多线程.zip
易语言实例模块——多线程例程-黑客帝国特效.e
Android源码——多线程断点下载.zip
第13章龟兔赛跑——多线程.ppt
IOS实例开发源码——多线程编程.zip
安卓Android源码——多线程断点下载.zip
Android源码——多线程断点下载源码.zip