java多线程学习笔记(六) ——线程间的通信(初接触)
2015-10-17 10:04
330 查看
线程间通讯:
多个线程在处理同一资源,但是任务却不同。
等待/唤醒机制。
涉及的方法:
1,wait(): 让线程处于冻结状态,被wait的线程会被存储到线程池中。
2,notify():唤醒线程池中一个线程(任意).
3,notifyAll():唤醒线程池中的所有线程。
这些方法都必须定义在同步中。
因为这些方法是用于操作线程状态的方法。
必须要明确到底操作的是哪个锁上的线程。
为什么操作线程的方法wait notify notifyAll定义在了Object类中?
因为这些方法是监视器的方法。监视器其实就是锁。
锁可以是任意的对象,任意的对象调用的方式一定定义在Object类中。
最终,进行代码的优化:
多个线程在处理同一资源,但是任务却不同。
//资源 class Resource { String name; String sex; } //输入 class Input implements Runnable { Resource r ; // Object obj = new Object(); Input(Resource r) { this.r = r; } public void run() { int x = 0; while(true) { synchronized(r) { if(x==0) { r.name = "mike"; r.sex = "nan"; } else { r.name = "丽丽"; r.sex = "女女女女女女"; } } x = (x+1)%2; } } } //输出 class Output implements Runnable { Resource r; // Object obj = new Object(); Output(Resource r) { this.r = r; } public void run() { while(true) { synchronized(r) { System.out.println(r.name+"....."+r.sex); } } } } class ResourceDemo { public static void main(String[] args) { //创建资源。 Resource r = new Resource(); //创建任务。 Input in = new Input(r); Output out = new Output(r); //创建线程,执行路径。 Thread t1 = new Thread(in); Thread t2 = new Thread(out); //开启线程 t1.start(); t2.start(); } }
等待/唤醒机制。
涉及的方法:
1,wait(): 让线程处于冻结状态,被wait的线程会被存储到线程池中。
2,notify():唤醒线程池中一个线程(任意).
3,notifyAll():唤醒线程池中的所有线程。
这些方法都必须定义在同步中。
因为这些方法是用于操作线程状态的方法。
必须要明确到底操作的是哪个锁上的线程。
为什么操作线程的方法wait notify notifyAll定义在了Object类中?
因为这些方法是监视器的方法。监视器其实就是锁。
锁可以是任意的对象,任意的对象调用的方式一定定义在Object类中。
//资源 class Resource { String name; String sex; boolean flag = false; } //输入 class Input implements Runnable { Resource r ; // Object obj = new Object(); Input(Resource r) { this.r = r; } public void run() { int x = 0; while(true) { synchronized(r) { if(r.flag) try{r.wait();}catch(InterruptedException e){} if(x==0) { r.name = "mike"; r.sex = "nan"; } else { r.name = "丽丽"; r.sex = "女女女女女女"; } r.flag = true; r.notify(); } x = (x+1)%2; } } } //输出 class Output implements Runnable { Resource r; // Object obj = new Object(); Output(Resource r) { this.r = r; } public void run() { while(true) { synchronized(r) { if(!r.flag) try{r.wait();}catch(InterruptedException e){} System.out.println(r.name+"....."+r.sex); r.flag = false; r.notify(); } } } } class ResourceDemo2 { public static void main(String[] args) { //创建资源。 Resource r = new Resource(); //创建任务。 Input in = new Input(r); Output out = new Output(r); //创建线程,执行路径。 Thread t1 = new Thread(in); Thread t2 = new Thread(out); //开启线程 t1.start(); t2.start(); } }
最终,进行代码的优化:
class Resource { private String name; private String sex; private boolean flag = false; public synchronized void set(String name,String sex) { if(flag) try{this.wait();}catch(InterruptedException e){} this.name = name; this.sex = sex; flag = true; this.notify(); } public synchronized void out() { if(!flag) try{this.wait();}catch(InterruptedException e){} System.out.println(name+"...+...."+sex); flag = false; notify(); } } //输入 class Input implements Runnable { Resource r ; // Object obj = new Object(); Input(Resource r) { this.r = r; } public void run() { int x = 0; while(true) { if(x==0) { r.set("mike","nan"); } else { r.set("丽丽","女女女女女女"); } x = (x+1)%2; } } } //输出 class Output implements Runnable { Resource r; // Object obj = new Object(); Output(Resource r) { this.r = r; } public void run() { while(true) { r.out(); } } } class ResourceDemo3 { public static void main(String[] args) { //创建资源。 Resource r = new Resource(); //创建任务。 Input in = new Input(r); Output out = new Output(r); //创建线程,执行路径。 Thread t1 = new Thread(in); Thread t2 = new Thread(out); //开启线程 t1.start(); t2.start(); } }
相关文章推荐
- java入门第七天 数组开个头 创建数组的几种方式和注意事项
- HDU1035 Robot Motion(java)
- Java字段初始化
- Spring3系列10- Spring AOP——Pointcut,Advisor拦截指定方法
- JDK环境变量设置
- java基础--数组操作
- 基于浏览器首选语言的springmvc和freemarker国际化配置的实现
- Struts实战-使用SSH框架技术开发学籍管理系统
- Struts2的处理流程
- Scanner用法详解
- 在进行maven+spring+struts+hibernate整合时发生class path resource [spring.xml] cannot be opened because it do
- Java中的math类
- Struts-Action
- java--通过DOM方式创建xml文件
- Java中如何遍历Map对象的4种方法
- 左右 Java 于 finally 深度分析语句块
- 普林斯顿大学算法第一部分学习总结(Week1-Percolation)
- Java中有关Null的9件事
- eclipse中怎么查看当前类的子类
- cucumber java从入门到精通(4)Scenario Outline及数据驱动