public class thread implements Runnable { Thread a = new Thread(this);
Thread b = new Thread(this); thread() {
a.start();
b.start();
} public static void main(String[] args) {
new thread(); } @Override
public void run() {
int x = 0;
while (x < 5) {
if (Thread.currentThread() == a)
System.out.println("a");
if (Thread.currentThread() == b)
System.out.println("b");
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
x++;
}
// TODO Auto-generated method stub }}以上程序我想用wait()使得a等待b完成后notify(),怎么改?

解决方案 »

  1.   

    wait()是Object的方法,意思是放弃已经拥有的当前对象的锁,然后在对象的等候队列上等待,让别的线程来唤醒它。
    有点像操作系统里面的“管程”的概念,做得比较优雅。
    可以作为通用的多线程编程模式,移植到C++等语言。
      

  2.   

    你是想唤醒那个?你先搞懂 notify的意思把
      

  3.   

    Object.wait()是不是就是把当前线程拥有的锁给拿回来,然后notify本身是标记的意思也就是说把这个锁标记给阻塞队列里面的第一个线程就相当与唤醒??
      

  4.   

    Object.wait和Object.notify是用于线程同步的,每个对象都有一把锁,线程运行遇到obj.wait()时就要获得obj的锁才能继续执行,否则会阻塞;如果遇到obj.notify()就会释放obj的锁,唤醒其他阻塞在obj对象上的线程
    这里只要给这个类增加阻塞对象就可以了
    public class thread implements Runnable {
        private Object obj=new Object();///用于同步线程
        Thread a = new Thread(this);
        Thread b = new Thread(this);    thread() {
            a.start();
            b.start();
        }    public static void main(String[] args) {
            new thread();    }    @Override
        public void run() {
            try{
                obj.wait();//获得obj的锁
                int x = 0;
                while (x < 5) {
                    if (Thread.currentThread() == a)
                        System.out.println("a");
                    if (Thread.currentThread() == b)
                        System.out.println("b");
                    Thread.sleep(1000);
                    x++;
                }
                obj.notify();//释放obj的锁
            }catch(Exception e){
                e.printStackTrace();
            }
        }}
      

  5.   

    public class thread implements Runnable {
        private Object obj=new Object();///用于同步线程
        Thread a = new Thread(this);
        Thread b = new Thread(this);    thread() {
            a.start();
            b.start();
        }    public static void main(String[] args) {
            new thread();    }    @Override
        public void run() {
            try{
                obj.wait();//获得obj的锁
                int x = 0;
                while (x < 5) {
                    if (Thread.currentThread() == a)
                        System.out.println("a");
                    if (Thread.currentThread() == b)
                        System.out.println("b");
                    Thread.sleep(1000);
                    x++;
                }
                obj.notify();//释放obj的锁
            }catch(Exception e){
                e.printStackTrace();
            }
        }}
      

  6.   

    报错..
    java.lang.IllegalMonitorStateException
    at java.lang.Object.wait(Native Method)
    at java.lang.Object.wait(Object.java:485)
    at thread.thread.run(thread.java:21)
    at java.lang.Thread.run(Unknown Source)
    java.lang.IllegalMonitorStateException
    at java.lang.Object.wait(Native Method)
    at java.lang.Object.wait(Object.java:485)
    at thread.thread.run(thread.java:21)
    at java.lang.Thread.run(Unknown Source)
      

  7.   

    顶一下,我看书上好像wait都要加在synchronized块里,我加了synchronized好像就没啥用。。求高手到底怎么使用wait
      

  8.   

    package PropertyChangeLis;public class thread implements Runnable { Thread a = new Thread(this);
    Thread b = new Thread(this); thread() {
    a.start();
    b.start();
    } public static void main(String[] args) {
    new thread(); } @Override
    public void run() {
    synchronized (this) { int x = 0;
    while (x < 5) {
    if (Thread.currentThread() == a)
    System.out.println("a");
    if (Thread.currentThread() == b)
    System.out.println("b");
    try {
    Thread.sleep(1000);
    } catch (InterruptedException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
    }
    x++;
    }
    // TODO Auto-generated method stub }
    }}