首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > JAVA > J2SE开发 >

请问一下,为什么小弟我无法叫醒wait的线程

2012-04-18 
请教一下,为什么我无法叫醒wait的线程?Java codepublic class ThreadTest3 {public static void main(Stri

请教一下,为什么我无法叫醒wait的线程?

Java code
public class ThreadTest3 {        public static void main(String[] args) throws InterruptedException {                new MyThread();        MyThread mt = new MyThread();        Thread t = new Thread(mt);                t.start();                                        for(int i = 0;i < 20; i++){            System.out.println("我是主线程。i:  " + i);            Thread.sleep(1000);            if(i == 10){new MyThread().call();}        }    }    }class MyThread implements Runnable{    Thread th1;    public void run() {        for(int i = 0;i < 10;i++){            System.out.println("我是二线程。i:  " + i);            try {                Thread.sleep(1000);            } catch (InterruptedException e) {                e.printStackTrace();            }            if(i == 5){                wat();            }        }            }        public synchronized void call()        notifyAll();    }        public synchronized void wat(){        try {            wait();        } catch (InterruptedException e) {            e.printStackTrace();        }    }        }


[解决办法]
Java code
public class ThreadTest3 {    public static void main(String[] args) throws InterruptedException {                new MyThread();        MyThread mt = new MyThread();        Thread t = new Thread(mt);                t.start();                                        for(int i = 0;i < 20; i++){            System.out.println("我是主线程。i:  " + i);            Thread.sleep(1000);            if(i == 10){                mt.call();            }        }    }    }class MyThread implements Runnable{    public void run() {        for(int i = 0;i < 10;i++){            System.out.println("我是二线程。i:  " + i);            try {                Thread.sleep(1000);            } catch (InterruptedException e) {                e.printStackTrace();            }            if(i == 5){                this.wat();            }        }    }        public synchronized void call(){        this.notifyAll();    }        public synchronized void wat(){        try {            this.wait();        } catch (InterruptedException e) {            e.printStackTrace();        }    } 

热点排行