如果我们在没有wait()方法的情况下使用notify()方法会发生什么? [英] What will happen if we use notify() method without wait() method ?

查看:503
本文介绍了如果我们在没有wait()方法的情况下使用notify()方法会发生什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

class abc extends Thread 
{
    @Override
    public void run ()
    {
        System.out.println(" hello ");    
        System.out.println(" world ");
   
        fg( ) ;
        
        System.out.println(" command ");
        System.out.println(" delete ");
    
        
    }
    
    synchronized void fg ()
    {
        try
        {
            
        Thread.sleep ( 1000 ) ;    
        notify () ;
        
        }
        catch ( InterruptedException e)
        {
            System.out.println(" caught ");
        } 
    }
    
}
public class using_notify 
{
    public static  void main(String[] args) 
    {
    abc w = new abc () ;
w.start () ;    
     
        
        System.out.println(" hi ");

    }
   
}





当那里的notify()如何工作是不是等待方法?

我在这里很困惑。请帮忙。



我尝试过的事情:



我跑了这个程序在Netbeans上。



How will notify() work here when there is no wait method ?
I am deeply confused here . Please help .

What I have tried:

I have run this program on Netbeans .

推荐答案

如果没有其他线程在等待,那么就不会发生太多事情。它试图通知等待的线程,如果可用,否则它什么也不做。



祝你好运!
If there is no other thread waiting then not much will happen. It tries to notify a waiting thread if available, otherwise it simply does nothing.

Good luck!


这篇关于如果我们在没有wait()方法的情况下使用notify()方法会发生什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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