java 为什么notify和notifyAll都不起作用?

class Example{

    public static void main(String arr[]){

        ThreadB th1=new ThreadB("th1");

        ThreadB th2=new ThreadB("th2");

        }

}

class ThreadB implements Runnable{

    C c;

    Thread thread;

    ThreadB(String name){

        c=new C();

        thread=new Thread(this,name);

        thread.start();

        }

    public void run(){

        if(thread.getName().equals("th1")){

            for(int i=0;i<3;i++)c.t1(false);

            c.t1(true);

            }

        if(thread.getName().equals("th2")){

            for(int i=0;i<3;i++)c.t2(false);

            c.t2(true);

            }

        System.out.println("end");

        }

}

class C{

    synchronized void t1(boolean boo){

            if(boo){

                notify();

                return;

                }

            System.out.println("t1");

            notify();

            try{

                wait();

                }catch(InterruptedException exc){System.out.println(exc);}

        }

    synchronized void t2(boolean boo){

            if(boo){

                System.out.println();notify();return;

                }

            System.out.println("t2");

            notify();//notifyAll()也没效果;

        }

}

调用了notify()无法唤醒th1线程。


慕娘9325324
浏览 965回答 2
2回答

叮当猫咪

每个线程初始化的时候都是 c=new C(),锁住的是不同的对象。class Example{&nbsp; &nbsp; public static void main(String arr[]){&nbsp; &nbsp; &nbsp; &nbsp; C c = new C();&nbsp; &nbsp; &nbsp; &nbsp; ThreadB th1=new ThreadB("th1",c);&nbsp; &nbsp; &nbsp; &nbsp; ThreadB th2=new ThreadB("th2",c);&nbsp; &nbsp; }}class ThreadB implements Runnable{&nbsp; &nbsp; C c;&nbsp; &nbsp; Thread thread;&nbsp; &nbsp; ThreadB(String name,C c){&nbsp; &nbsp; &nbsp; &nbsp; //c=new C();&nbsp; &nbsp; &nbsp; &nbsp; this.c=c;&nbsp; &nbsp; &nbsp; &nbsp; thread=new Thread(this,name);&nbsp; &nbsp; &nbsp; &nbsp; thread.start();&nbsp; &nbsp; }&nbsp; &nbsp; public void run(){&nbsp; &nbsp; &nbsp; &nbsp; if(thread.getName().equals("th1")){&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; for(int i=0;i<3;i++)c.t1(false);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; c.t1(true);&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; if(thread.getName().equals("th2")){&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; for(int i=0;i<3;i++)c.t2(false);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; c.t2(true);&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; System.out.println("end");&nbsp; &nbsp; }}class C{&nbsp; &nbsp; synchronized void t1(boolean boo){&nbsp; &nbsp; &nbsp; &nbsp; if(boo){&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; notify();&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; return;&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; System.out.println("t1");&nbsp; &nbsp; &nbsp; &nbsp; notify();&nbsp; &nbsp; &nbsp; &nbsp; try{&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; wait();&nbsp; &nbsp; &nbsp; &nbsp; }catch(InterruptedException exc){System.out.println(exc);}&nbsp; &nbsp; }&nbsp; &nbsp; synchronized void t2(boolean boo){&nbsp; &nbsp; &nbsp; &nbsp; if(boo){&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; System.out.println();notify();return;&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; System.out.println("t2");&nbsp; &nbsp; &nbsp; &nbsp; notify();//notifyAll()也没效果;&nbsp; &nbsp; }}

慕丝7291255

这两个线程wait的对象C不是同一个,是感知不到对方的锁的
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Java