小编Evg*_*lev的帖子

等待/通知的奇怪java行为

我发现java并发的奇怪行为.请参阅下面的代码:


    public class Test {
        static CountDownLatch latch = new CountDownLatch(1);
        public static void main(String[] args) throws UnsupportedEncodingException, InterruptedException {
            final Thread t = new MyThread();
            t.start();
            synchronized (t) {
                latch.countDown();
                System.out.println("got to sleep");
                t.wait();
                System.out.println("wake up");
            }
        }

        static class MyThread extends Thread {
            @Override
            public void run() {
                try {
                    latch.await();
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                synchronized (this) {
                    System.out.println("inside run");
    //                notifyAll();
                }
            }
        }
    }

在我看来,这段代码应该挂起并永远等待,但代码完成后没有任何问题,在控制台中下一步:

got to sleep
inside run
wake up

我试图找到一些关于通知锁的信息,如果线程已经死亡,但缺乏它.另外我在java规范中找不到任何信息. …

java concurrency multithreading

4
推荐指数
1
解决办法
573
查看次数

标签 统计

concurrency ×1

java ×1

multithreading ×1