这种情况会导致死锁吗? [英] Can this scenario cause a deadlock?

查看:104
本文介绍了这种情况会导致死锁吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我们有以下模式

 lock =  new  reentrantlock(真正); 

...

if (x = 1
{
执行 {
cond.lock()} (x = 1 );
}



假设有多个线程。



可能是饥饿情景?



我们只要条件为真就调用cond.lock,如果条件总是为真,那么只要信号()条件为真,就会导致饥饿方法被调用,它也会导致饥饿。我对吗?任何解决方案?

解决方案

非常糟糕,错误的模式不会导致死锁,但却是CPU时间的总浪费。此外, x 应该用作共享变量(否则循环永远不会中断),并且对它的访问不会互锁,而 cond.lock 简直无关紧要。浪费CPU时间可能会也可能不会导致饥饿,但很明显,您使用与浪费CPU时间相关的错误线程模式越​​多,饥饿就越可能。 :-)



无需修复此代码;整个想法都是错的。也许,你只想让一个线程等待,直到其他线程让它继续。这基本上是通过使用Java等待/通知机制完成的。例如,请参阅: http://www.javamex.com/tutorials/synchronization_wait_notify.shtml [ ^ ]。



另见本文: http://tutorials.jenkov.com/java-concurrency/thread-signaling .html [ ^ ] 。



在某种情况下你应该选择的机制实际上取决于使线程等待的目的。如果你想让一些线程等待另一个线程完成某些操作,可以通过使用类 CountDownLatch 来实现:

http://docs.oracle.com/javase/7/docs/ api / java / util / concurrent / CountDownLatch.html [ ^ ]。



这是一个类似的同步类,在一组线程上运行可重置计数: http://docs.oracle.com/javase/7/docs/ api / java / util / concurrent / CyclicBarrier.html [ ^ ]。



注意等待线程不会浪费任何CPU ti我。该方法实际上基于抢占机制(通过硬件中断在线程被抢占时触发事件)和OS中的线程调度,该线程调度映射在正确实现的Java机器操作上。线程处于等待状态,并且在通过设置某个条件唤醒之前不会被调度回执行。



-SA

we have the following pattern

lock = new reentrantlock(true);

...

if(x = 1)
{
do{
cond.lock()}while(x = 1);
}


assume there are multiple threads.

is a starvation scenario possible?

we're calling cond.lock as long as condition is true, if condition is always true, it will lead to a starvation, if condition is true whenever the signal() method is called, it will also lead to a starvation. am i right? any solution to this?

解决方案

Very bad, wrong pattern which does not cause deadlock but is the total waste of CPU time. Besides, x is supposed to be used as a shared variable (otherwise it the loop will never break), and the access to it is not interlocked, while cond.lock is simply irrelevant. Wasting of CPU time may or may not lead to "starvation", but it is obvious that the more you use wrong threading patterns related to the waste of CPU time, the more likely the starvation can be. :-)

No need to fix this code; the whole idea is wrong. Probably, you simply want to keep one thread waiting until some other thread let it go. This is basically done by using Java wait/notify mechanism. See for example: http://www.javamex.com/tutorials/synchronization_wait_notify.shtml[^].

See also this article: http://tutorials.jenkov.com/java-concurrency/thread-signaling.html[^].

The mechanism you should chose in a certain situation really depends on the purpose of making thread waiting. If you want to keep some thread waiting until some set of operation is complete by another thread, this can be achieved through using the class CountDownLatch:
http://docs.oracle.com/javase/7/docs/api/java/util/concurrent/CountDownLatch.html[^].

This is a similar synchronization class with resettable count operating on a set of threads: http://docs.oracle.com/javase/7/docs/api/java/util/concurrent/CyclicBarrier.html[^].

Note that the waiting thread does not waste any CPU time. The approach is actually based on the mechanism of preemption (through hardware interrupts triggering events when a thread could be preempted) and thread scheduling in the OS which is mapped on the properly implemented Java machine operation. A thread is put in the wait state and is not scheduled back to execution until it is waken up by setting certain condition.

—SA


这篇关于这种情况会导致死锁吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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