首页 > 其他分享 >wait、notify、notifyAll

wait、notify、notifyAll

时间:2023-04-24 14:45:54浏览次数:23  
标签:notifyAll System start notify println wait out

介绍

wait 方法前提需要拥有锁。使用wait方法后,释放锁进行等待队列。

notify 方法从等待队列移除一个元素。

notifyAll 将等待队列中元素全部进行移出。

注意:notify、notifyAll 会等代码执行完才会释放锁

@Test
    public void threadTest() {
        
        new Thread(() -> {
            synchronized (this) {
                System.out.println("A-start");
                try {
                    wait();
                } catch (InterruptedException e) {
                    throw new RuntimeException(e);
                }
                System.out.println("A-over");
            }
        }).start();
        
        new Thread(() -> {
            synchronized (this) {
                System.out.println("B-start");
                notifyAll();
                System.out.println("B-over");
            }
        }).start();

    }

标签:notifyAll,System,start,notify,println,wait,out
From: https://www.cnblogs.com/handsometaoa/p/17349461.html

相关文章

  • JAVA wait(), notify(),sleep详解
    开了博客后,一直也没在上面发布过文章,直到前一段时间与一位前辈的对话,才发现技术博客的重要,立志要把博客建好。但一直没有找到好的开篇的主题,今天再看JAVA线程互斥、同步的时候又有了新的体会,就以他作为开篇吧。   在JAVA中,是没有类似于PV操作、进程互斥等相关的方法的。JAVA的......
  • cpp condition_variable wait_until unique_mutex time_out
    #include<chrono>#include<condition_variable>#include<ctime>#include<fstream>#include<future>#include<iomanip>#include<iostream>#include<map>#include<mutex>#include<sstream>#in......
  • 【线程基础】【五】yield、sleep、wait方法的异同
    1 前言本节我们讲下Thread.yield()方法的作用,并对比下sleep()方法、wait()方法,看看三者有何区别。2 yield方法作用是:提出释放CPU时间片的请求,不会释放锁,主要知道它不会阻塞线程即可哈,平时我是没怎么用过哈。特点就是:(1)yield()方法只是提出申请释放CPU资源,至于能否成功释放......
  • C# Async,Await简单理解(应用层次)
    这里我就不给大家去研究这两上下文关键字的il实现了,感兴趣的朋友可以自己去用ildasm分析一下。我写这篇文章只是想强调微软官方文档上写的一点: async和await并没有开辟新的线程,我们来分别执行一下io密集型和cpu密集型代码 :staticvoidMain(string[]args){Stopwatchst......
  • Oracle等待事件(二)—— free buffer wait 常见原因及对应解决方法
    首先看看官方文档中的描述Thiswaiteventindicatesthataserverprocesswasunabletofindafreebufferandhaspostedthedatabasewritertomakefreebuffersbywritingoutdirtybuffers.Adirtybufferisabufferwhosecontentshavebeenmodified.Dirtyb......
  • 《Ubuntu — NetworkManager开机提示A start job is running for Network Manager wai
    轉自:https://www.cnblogs.com/zhuangquan/p/13209758.html,僅供參考學習使用1.NetworkManagerUbuntuServer:Ubuntu的Server版本只有终端界面,没有桌面GUI,且Server版本不会安装NetworkManager,所以UbuntuServer网络由配置文件进行配置。由于Server版本一般用作服务器的......
  • JavaScript 利用 async await 实现 sleep 效果
    constsleep=(timeountMS)=>newPromise((resolve)=>{setTimeout(resolve,timeountMS);});(async()=>{console.log('11111111,'+newDate());awaitsleep(2000);console.log('22222222,'+newDate());await......
  • 关于mysql报 loopWaitCount 0, wait millis 60001 错误的解决办法
    最近遇到个比较奇怪的问题,系统上线一段时间之后,总是隔一段时间就出现Tomcat连接数据库报错,导致系统无法运行。通过日志排查发现,里面报了一个错误,内容如下:12023-04-1700:01:05[ERROR][AcquireJobsRunnableImpl.java:77(run)]exceptionduringjobacquisition:Could......
  • Go 每日一库之 fsnotify
    上一篇文章Go每日一库之viper中,我们介绍了viper可以监听文件修改进而自动重新加载。其内部使用的就是fsnotify这个库,它是跨平台的。今天我们就来介绍一下它。快速使用先安装:$gogetgithub.com/fsnotify/fsnotify后使用:packagemainimport("log""github.com/......
  • ts类型体操 Awaited
    /*189-Awaited-------byMaciejSikora(@maciejsikora)#easy#promise#built-in###QuestionIfwehaveatypewhichiswrappedtypelikePromise.Howwecangetatypewhichisinsidethewrappedtype?Forexample:ifwehave`Promise&l......