Java Synchronized与wait notify 连用
2015-07-24 14:51
399 查看
Java 两个线程同步感悟
//学习知识一定要学透彻,否则你你只是在一味地记忆知识
我查看API 看到wait() 有这么一段说明: The current thread must own this object's monitor.
一直没有明白这句话倒是是什么意思。后来做了这个实例。刚开始我是这样写的程序
编译器给我报的是这个异常java.lang.IllegalMonitorStateException.Google一下没怎么明白,我又想起上面 wait的那段解释,就讲程序进行了修正。最终结果就正确的。这我说明你用wait,notify,notifyall,必须和synchronized 锁同一个对象
//学习知识一定要学透彻,否则你你只是在一味地记忆知识
import java.util.Date; import java.util.concurrent.TimeUnit; import java.util.concurrent.locks.Condition; import java.util.concurrent.locks.Lock; import javax.swing.text.StyledEditorKit.ForegroundAction; import org.omg.CORBA.PUBLIC_MEMBER; class Resource{ String name; String sex; boolean flag = false; } class Input implements Runnable { Resource resource = new Resource(); public Input(Resource resource){ this.resource = resource; } public void run() { boolean flag = true; while(true){ synchronized(resource){ if(resource.flag){ try { this.resource.wait(); } catch (InterruptedException e) { // TODO Auto-generated catch block e.printStackTrace(); } } if(flag){ this.resource.name = "mike"; this.resource.sex = "man"; flag = false; } else{ this.resource.name = "Lili"; this.resource.sex = "femal"; flag = true; } this.resource.notify(); resource.flag = true; }// synchronized }//while } } class Output implements Runnable { Resource resource = new Resource(); public Output(Resource resource) { // TODO Auto-generated constructor stub this.resource = resource; } @Override public void run() { // TODO Auto-generated method stub while(true){ synchronized (resource) { if(!resource.flag){ try { this.resource.wait(); } catch (InterruptedException e) { // TODO Auto-generated catch block e.printStackTrace(); } System.out.println(resource.name + "..." + resource.sex); resource.flag = false; resource.notify(); } } } } } public class HeiMa { public static void main(String[] args){ Resource resource = new Resource(); Input input = new Input(resource); Output output = new Output(resource); Thread t1 = new Thread(input); Thread t2 = new Thread(output); t1.start(); t2.start(); } }
我查看API 看到wait() 有这么一段说明: The current thread must own this object's monitor.
一直没有明白这句话倒是是什么意思。后来做了这个实例。刚开始我是这样写的程序
import java.util.Date; import java.util.concurrent.TimeUnit; import java.util.concurrent.locks.Condition; import java.util.concurrent.locks.Lock; import javax.swing.text.StyledEditorKit.ForegroundAction; import org.omg.CORBA.PUBLIC_MEMBER; class Resource{ String name; String sex; boolean flag = false; } class Input implements Runnable { Resource resource = new Resource(); public Input(Resource resource){ this.resource = resource; } public void run() { boolean flag = true; while(true){ synchronized(resource.getClass()){ if(resource.flag){ try { this.resource.wait(); } catch (InterruptedException e) { // TODO Auto-generated catch block e.printStackTrace(); } } if(flag){ this.resource.name = "mike"; this.resource.sex = "man"; flag = false; } else{ this.resource.name = "Lili"; this.resource.sex = "femal"; flag = true; } this.resource.notify(); resource.flag = true; }// synchronized }//while } } class Output implements Runnable { Resource resource = new Resource(); public Output(Resource resource) { // TODO Auto-generated constructor stub this.resource = resource; } @Override public void run() { // TODO Auto-generated method stub while(true){ synchronized (resource.getClass()) { if(!resource.flag){ try { this.resource.wait(); } catch (InterruptedException e) { // TODO Auto-generated catch block e.printStackTrace(); } System.out.println(resource.name + "..." + resource.sex); resource.flag = false; resource.notify(); } } } } } public class HeiMa { public static void main(String[] args){ Resource resource = new Resource(); Input input = new Input(resource); Output output = new Output(resource); Thread t1 = new Thread(input); Thread t2 = new Thread(output); t1.start(); t2.start(); } }
编译器给我报的是这个异常java.lang.IllegalMonitorStateException.Google一下没怎么明白,我又想起上面 wait的那段解释,就讲程序进行了修正。最终结果就正确的。这我说明你用wait,notify,notifyall,必须和synchronized 锁同一个对象
相关文章推荐
- Java基础语法(一)
- JDK安装和Eclipse介绍
- [LeetCode][Java] Pascal's Triangle II
- eclipse 到 android studio的一些快捷键
- 问题三20150724java.io.InvalidClassException:local class incompatible: stream classdesc
- java不定参数存在一个小问题。
- 如何在Eclipse里面更新添加主题
- ElasticSearch在window下的基本配置
- switch(String s)是在jdk1.7之后才出现,但是编译器又要求使用较低版本。
- dubbo(分布式架构)+zookeeper(注册中心)+spring(provider和consumer) 基本使用
- java.util.concurrent.ExecutionException: java.lang.OutOfMemoryError: Java heap space
- 在spring中使用MethodInterceptor实现aop(附项目中demo)
- JAVA抽象类和接口的理解
- Struts2注解 特别注意
- java实现从网上下载图片到本地的方法
- [LeetCode][Java] Pascal's Triangle
- JAVA的比较之==
- Java 中最常见的 5 个错误
- Spring 整合 Hibernate
- Eclipse中安装subclipse插件