java怎样将多个线程放入到一个对象等待池中

Python010

java怎样将多个线程放入到一个对象等待池中,第1张

TaskQueue Extends Queue

{

object taskLock = null

Runnable Dequeue ()

{

synchronized (taskLock)

{

while (getCount () <= 0)

{

taskLock.Wait ()

}

return super.dequeue ()

}

}

void Enqueue (Runnable runObject)

{

synchronized (taskLock)

{

super.enqueue (runObject)

taskLock.NotifyAll ()

}

}

}

sleep和wait的区别:

1、sleep的意思是:睡,睡觉,睡眠。

2、wait的意思是:等候,推迟,延缓等待,耽搁,伺候用餐。

拓展资料

sleep的用法

1、They were exhausted from lack of sleep

由于缺乏睡眠,他们非常疲惫。

2、During the car journey, the baby slept

坐车来的路上,宝宝睡着了。

3、I think he may be ready for a sleep soon.

我想他也许很快就要睡一觉了。

4、I can't get to sleep with all that singing.

那些歌声搅得我无法入睡。

5、I didn't lose too much sleep over that investigation.

我并不太担心那个调查。

wait

1、I walk to a street corner and wait for the school bus

我走到街角等校车。

2、There'll be a car waiting for you

会有辆汽车等你。

3、I want to talk to you, but it can wait

我想和你谈谈,但可以晚点再说。

4、If you think this all sounds very exciting, just wait until you read the book

如果你觉得所有这些听起来令人兴奋,那就等着去读这本书吧。

5、'Wait a minute!' he broke in. 'This is not giving her a fair hearing!'

“等一下,”他插嘴说,“这没有给她一个公平的解释机会!”

wait中的线程是不会去竞争对象锁的。

据我所知,开始由于调用了对象的wait方法,线程处于该对象的等待池中

而后,只有再去调用对象的notifyAll()(唤醒所有等待池中的线程)或者notify()(随机唤醒线程,姑且假设唤醒了我们的那个线程),线程会进入该对象的锁池之中。

锁池中的对象相互竞争对象锁,优先级高的线程竞争得到对象锁的概率高,假若线程没有竞争到,它还是会在锁池之中,唯有线程再次调用wait方法,它才会重新回到等待池中。

希望对你有帮助。