这种用法是jdk1.5以前的用法,jdk1.5之后用lock和Condition可以参考我这篇博文http://blog.csdn.net/arryluo123/article/details/55669360
/**
* wait; notify(); notifyAll(); 都使用在同步中,因为要持有监视器(锁)的线程操作;<br>
* 所以要使用在同步中,因为只有同步操作才有锁.<br>
* 为什么这些操作线程的方法要定义在object中呢??? 因为这些方法在操作同步中线程时,都必须要标识它们所操作的线程持有的锁<br>
* 只有同一个锁上的被等待线程,可以被同一个锁上的notify唤醒<br>
* 不可以对不同锁中的线程进行唤醒<br>
* 也就是说,等待和唤醒必须是同一个锁;<br>
* 而锁可以是任意对象,所以可以被任意对象调用,因为该方法定义object类中;
*
*
*/
class Res {
String name;
String sex;
boolean flg = false;
}
class InputThread implements Runnable {
private Res r;
public InputThread(Res r) {
this.r = r;
}
@Override
public void run() {
// TODO Auto-generated method stub
int i = 0;
while (true) {
// 循环存入
// 这样写有一个线程不同步问题,
/**
* 解决方式是:<br>
* 加一个同步锁,但加同步锁的前提是必须得两个线程用同一把锁,
*/
synchronized (r) {
// 如果flg为true,则说明有数据,那么我就让线程等待,让输出的线程进行输出;
if (r.flg)
try {
//System.out.println("1" + r.flg);
r.wait();
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
if (i == 0) {
r.name = "jeke";
r.sex = "man";
} else {
r.name = "哈哈";
r.sex = "女女女";
}
i = (i + 1) % 2;
// 当执行到这一步来时,说明是有数据的
r.flg = true;
// 将通知线程,让他启动
r.notify();
}
}
}
}
// 取出数据
class OutPutThread implements Runnable {
private Res r;
public OutPutThread(Res r) {
this.r = r;
}
@Override
public void run() {
// TODO Auto-generated method stub
while (true) {
// 在同步线程里加入这个对象,就可以保证锁是同一把锁
synchronized (r) {
// 如果flg为true说明,在输入的线程中进行存了数据,那么我就吧这个数据取出来.
if (r.flg) {
System.out.println(r.name + "....." + r.sex);
// 当取出来之后,我就吧这个数据设置为false,
r.flg = false;
r.notify();// 然后通知输入线程,你可以输入数据了
}
try {
r.wait();// 当没有数据时此时的输出的线程在等待输入线程输入的内容过来;
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}
}
public class MoreThread {
public static void main(String[] args) {
Res r = new Res();
InputThread inputThread = new InputThread(r);
OutPutThread outPutThread = new OutPutThread(r);
new Thread(inputThread).start();
new Thread(outPutThread).start();
}
}