wait
notify ()
nitifyAll ()
都使用在同步中,因为要对持有监视器(锁)的线程操作
所以要使用在同步中,因为只有同步才具有锁
为什么这些操作线程的方法要定义object类中呢
因为这些方法在操作同步中线程时。都必须要标识他们所操作线程只有的锁
只有同一个锁上的被等待线程,可以被同一个锁上的notify唤醒
不可以对不同锁中的线程进行唤醒
也就是说,等待和唤醒必须是同一个锁
而锁可以使任意对象,所以可以被任意对象调用的方法定义object类中
class Res
{
String name;
String sex;
boolean flag = false;
} class Input implements Runnable
{
private Res r ;
Input (Res r){
this.r = r;
}
public void run (){
int x = 0;
while (true)
{
synchronized (r) {
if (r.flag)
try {r.wait();}catch(Exception e){}
if (x==0){
r.name = "java";
r.sex = "man";
}
else
{
r.name = "杰克";
r.sex = "女女vn"; }
x = (x+1)%2;
r.flag = true;
r.notify(); }
} } } class Output implements Runnable
{
private Res r;
Output (Res r){ this.r = r;
}
public void run (){
while (true){
synchronized(r){ if (!r.flag) try {r.wait();}catch (Exception e){}
System.out.println (r.name+"..."+r.sex);
r.flag = false;
r.notify(); }
} } } public class InputOutputDemo { public static void main(String[] args) {
// TODO Auto-generated method stub System.out.println ("ddddd");
Res r = new Res ();
Input input = new Input (r);
Output output = new Output (r); Thread t1 = new Thread (input);
Thread t2 = new Thread (output);
t1.start();
t2.start();
} }
在1.5之后出现了lock关键字因此使用这个关键字丰富和我们平时开发习惯
同时Condition 取代了wait notifly nitiflyAll
import java.util.concurrent.locks.ReentrantLock;
import java.util.concurrent.locks.Condition;
import java.util.concurrent.locks.Lock; public class one { public static void main(String[] args) {
// TODO Auto-generated method stub
System.out.println("ddd");
RRRRRRR res = new RRRRRRR(); Producer p1 = new Producer(res);
Producer p2 = new Producer(res); consumers c1 = new consumers(res);
consumers c2 = new consumers(res); Thread t1 = new Thread(p1);
Thread t2 = new Thread(p2);
Thread t3 = new Thread(c1);
Thread t4 = new Thread(c2); t1.start();
t2.start();
t3.start();
t4.start();
} }
class RRRRRRR
{
private String name;
private int count = 1;
private boolean flag = false; private Lock lock = new ReentrantLock();
private Condition condition_pro = lock.newCondition();
private Condition condition_con = lock.newCondition();
public void set (String name) throws InterruptedException{
lock.lock();
try{
while (flag)
condition_pro.await();
this .name = name + "--" + count++;
System.out.println(Thread.currentThread().getName()+"生产商品"+this.name);
flag = true;
condition_con.signal();
}
finally{
lock.unlock();
}
} public void out()throws InterruptedException {
lock.lock();
try{
while (!flag)
condition_con.await();
System.out.println(Thread.currentThread().getName() + "消费了" + this.name);
flag = false;
condition_pro.signal();
}
finally{
lock.unlock();
}
}
}
/*
* 生产者
*/
class Producer implements Runnable
{
private RRRRRRR res;
Producer (RRRRRRR res){ this.res = res;
}
public void run(){ try{
while (true){ res.set ("牙膏");
}
}
catch(InterruptedException e){ }
} }
/*
* 消费者
*/
class consumers implements Runnable
{ private RRRRRRR res;
consumers(RRRRRRR res){ this.res = res;
}
public void run(){
try{ while (true){
res.out();
}
}
catch(InterruptedException e){ }
}
}