Java等待线程在通知后不会恢复 [英] Java waiting Thread doesn't resume after notify

查看:53
本文介绍了Java等待线程在通知后不会恢复的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我下面的程序有 2 个线程 T1 和 T2.T1 首先运行并进入等待状态.T2 正在呼叫通知.为什么 T1 线程没有恢复并打印Thread-0 被唤醒"

I have below program having 2 threads T1 and T2. T1 is running first and goes into waiting state. T2 is calling notify. Why T1 thread doesn't resumes and prints "Thread-0 is waken up"

public class WaitNotifyTest{
    public static void main(String[] args) {
        Thread t1 = new Thread(new Runnable() {
            @Override
            public void run() {
                synchronized (this) {
                    System.out.println(Thread.currentThread().getName() + " is running");
                    try {
                        wait();
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                    System.out.println(Thread.currentThread().getName() + " is waken up");
                }
            }
        });
        Thread t2 = new Thread(new Runnable() {
            @Override
            public void run() {
                synchronized (this) {
                    try {
                        Thread.sleep(3000);
                        System.out.println(Thread.currentThread().getName() + " is running");
                        notify();
                        System.out.println(Thread.currentThread().getName() + " notifying");
                    } catch (InterruptedException e1) {
                        e1.printStackTrace();
                    }
                }
            }
        });
        t1.start();
        t2.start();
    }
}

输出为

Thread-0 is running
Thread-1 is running
Thread-1 notifying

请注意,在此输出之后,我的程序不会结束/终止.任何人都可以解释为什么我的程序没有终止.

Note after this output my program doesn't ends/terminate. Can anyone please explain why my program not getting terminate.

推荐答案

你的代码中有两个问题1.您应该使用相同的对象在线程之间进行通信.2.调用等待并通知您锁定的同一对象.

two issue is there in your code 1.You should use same object to communicate between the thread. 2.call wait and notify on same object on which you have taken the lock.

public class WaitNotifyTest{


    public static void main(String[] args) {
        Object lock=new Object(); 
            Thread t1 = new Thread(new Runnable() {
                @Override
                public void run() {
                    synchronized (lock) {
                        System.out.println(Thread.currentThread().getName() + " is running");
                        try {
                            lock.wait();
                        } catch (InterruptedException e) {
                            e.printStackTrace();
                        }
                        System.out.println(Thread.currentThread().getName() + " is waken up");
                    }
                }
            });
            Thread t2 = new Thread(new Runnable() {
                @Override
                public void run() {
                    synchronized (lock) {
                        try {
                            Thread.sleep(3000);
                            System.out.println(Thread.currentThread().getName() + " is running");
                            lock.notify();
                            System.out.println(Thread.currentThread().getName() + " notifying");
                        } catch (InterruptedException e1) {
                            e1.printStackTrace();
                        }
                    }
                }
            });
            t1.start();
            t2.start();
        }
    }

这篇关于Java等待线程在通知后不会恢复的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆