多线程中wait和sleep区别

版权声明:关注微信公众号:摸鱼科技资讯,联系我们 https://blog.csdn.net/qq_36949176/article/details/83510051

wiat和sleep的区别?

1、wait可以指定时间也可以不指定

     sleep必须指定时间。

2、在同步中,对cpu的执行权和锁的处理不同

wait:释 放执行权,释放锁。

sleep:释放执行权,不释放锁。

class Demo
{
    void show ()
    {
        synchronize(this)
        {
            wait();//to t1 t2
        }
    }
    void method()
    {
        synchronize(this)
        {
            //wait();
            notifyAll();
        }
    }
}

猜你喜欢

转载自blog.csdn.net/qq_36949176/article/details/83510051