您的位置:首页 > 编程语言 > Java开发

Java Synchronized与wait notify 连用

2015-07-24 14:51 399 查看
Java 两个线程同步感悟

//学习知识一定要学透彻,否则你你只是在一味地记忆知识

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 锁同一个对象
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: