/**
多线程间通讯实例
*/
class Resource
{
private String name;
private String sex;
private boolean flag = false;
public synchronized void set(String name, String sex)
{
if (flag == true)
{
try
{
this.wait();
}
catch(InterruptedException e)
{
}
}
this.name = name;
this.sex = sex;
flag = true;
this.notify();
}
public synchronized void out()
{
if (flag != true)
{
try
{
this.wait();
}
catch (InterruptedException e)
{
}
System.out.println(name+"..+.."+sex);
flag = false;
notify();
}
}
}//=======================================
class Input implements Runnable
{
Resource r;
Input(Resource r)
{
this.r = r;
}
public void run()
{
int x = 0;
while(true)
{ if (x == 0)
{
r.set("Mike", "Male");
}
else
{
r.set("丽丽", "女女女女");
} x =(x + 1) % 2;
}
}
}
//============================================
class Output implements Runnable
{
Resource r;
Output(Resource r)
{
this.r = r;
}
public void run()
{
while(true)
{
r.out();
}
}
}
//=======================================
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();
}
}我不知道哪里出了问题,形成了死锁。请高手帮忙指点一下。谢谢。
多线程间通讯实例
*/
class Resource
{
private String name;
private String sex;
private boolean flag = false;
public synchronized void set(String name, String sex)
{
if (flag == true)
{
try
{
this.wait();
}
catch(InterruptedException e)
{
}
}
this.name = name;
this.sex = sex;
flag = true;
this.notify();
}
public synchronized void out()
{
if (flag != true)
{
try
{
this.wait();
}
catch (InterruptedException e)
{
}
System.out.println(name+"..+.."+sex);
flag = false;
notify();
}
}
}//=======================================
class Input implements Runnable
{
Resource r;
Input(Resource r)
{
this.r = r;
}
public void run()
{
int x = 0;
while(true)
{ if (x == 0)
{
r.set("Mike", "Male");
}
else
{
r.set("丽丽", "女女女女");
} x =(x + 1) % 2;
}
}
}
//============================================
class Output implements Runnable
{
Resource r;
Output(Resource r)
{
this.r = r;
}
public void run()
{
while(true)
{
r.out();
}
}
}
//=======================================
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();
}
}我不知道哪里出了问题,形成了死锁。请高手帮忙指点一下。谢谢。
package com.demo;/**
多线程间通讯实例
*/
class Resource
{
private String name;
private String sex;
private boolean flag = false; public synchronized void set(String name, String sex)
{
if (flag == true)
{
try
{
this.wait();
}
catch(InterruptedException e)
{
}
}
this.name = name;
this.sex = sex;
flag = true;
this.notify();
} public synchronized void out()
{
if (flag != true)
{
try
{
this.wait();
}
catch (InterruptedException e)
{
} System.out.println(name+"..+.."+sex); flag = false;
notify();
}
}
}//=======================================
class Input implements Runnable
{
Resource r; Input(Resource r)
{
this.r = r;
} public void run()
{
int x = 0;
while(true)
{
System.out.println("in");
if (x == 0)
{
r.set("Mike", "Male");
}
else
{
r.set("丽丽", "女女女女");
} x =(x + 1) % 2;
}
}
}
//============================================
class Output implements Runnable
{
Resource r;
Output(Resource r)
{
this.r = r;
} public void run()
{
while(true)
{
System.out.println("out");
r.out();
} }
}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();
}
}你看输出,代码是没有死锁的
同步方法,是用当前对象作为锁,也就是用当前Resource 对象作为锁。而你没个线程里都有一个Resource对象,这是两个不同的对象(换在这里是有两把锁),他们各自调set()或者out()方法时,是把自己作为锁。而notify()方法只能唤醒使用相同锁的线程。所以你这个案例肯定会死锁。