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

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

问题描述

我下面有两个线程T1和T2的程序. T1首先运行并进入等待状态. T2正在呼叫通知.为什么T1线程无法恢复并显示线程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.call等待并通知您已获取锁定的同一对象.

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天全站免登陆