栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

java多线程学习笔记之8 什么是死锁

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

java多线程学习笔记之8 什么是死锁

Simplistically, deadlock occurs when two or more threads are waiting for two or more locks to be freed and the circumstances in the program are such that the locks are never freed. Interestingly, it is possible to deadlock even if no synchronization locks are involved. A deadlock situation involves threads waiting for conditions; this includes waiting to acquire a lock and also waiting for variables to be in a particular state. On the other hand, it is not possible to deadlock if only one thread is involved, as Java allows nested lock acquisition. If a single user thread deadlocks, a system thread must also be involved.

简单地说,当两个或多个线程等待释放两个或多个锁,并且程序中的情况是锁永远不会被释放时,就会发生死锁。有趣的是,即使不涉及同步锁,也可能出现死锁。死锁情况涉及线程等待条件;这包括等待获取锁,以及等待变量处于特定状态。另一方面,如果只涉及一个线程,就不可能死锁,因为Java允许嵌套锁获取。如果单用户线程死锁,则还必须涉及系统线程。

一般来说,死锁是很难解决的.一个死锁有可能会让开发者无法不通过全部重新设计已经编写好的程序来解决.就因为复杂到了这种程度,所以死锁通常是不可能轻松解决的,或是某种程度上仅能够期待由底层的系统自动地解决掉死锁问题.对于开发者来说唯一可以做的是预防写出能够造成死锁的程序,才是首要任务,也是可以把握的事情.

Can the system somehow resolve this deadlock, just as it is able to avoid the potential deadlock when a thread tries to grab the same lock again? Unfortunately, this problem is different. Unlike the case of the nested locks, where a single thread is trying to grab a single lock multiple times, this case involves two separate threads trying to grab two different locks. Since a thread owns one of the locks involved, it may have already made changes that make it impossible for it to free the lock. To be able to fix this problem at the system level, Java would need a system where the first lock can't be grabbed until it is safe from deadlock or provide a way for the deadlock to be resolved once it occurs. Either case is very complex and may be more complex than just having the developer design the program correctly 

系统能否以某种方式解决这个死锁,就像它能够避免线程再次尝试获取同一个锁时可能出现的死锁一样?不幸的是,这个问题是不同的。与嵌套锁的情况不同,在嵌套锁中,一个线程试图多次获取一个锁,而这种情况涉及两个单独的线程试图获取两个不同的锁。由于一个线程拥有其中一个锁,它可能已经进行了更改,无法释放锁。为了能够在系统级解决这个问题,Java需要一个系统,在这个系统中,第一个锁在安全解除死锁之前不能被抓取,或者在死锁发生后提供一种解决死锁的方法。这两种情况都非常复杂,可能比让开发人员正确设计程序更复杂 

In general, deadlocks can be very difficult to resolve. It is possible to have a deadlock that developers can't fix without a complete design overhaul. Given this complexity, it is not possible, or fair, to expect the underlying system to resolve deadlocks automatically. As for the developer, we look at the design issues related to deadlock prevention and even develop a tool that can be used to detect a deadlock 

一般来说,僵局(死锁)可能很难解决。如果没有彻底的设计改革,开发人员可能无法修复死锁。鉴于这种复杂性,期望底层系统自动解决死锁是不可能的,也不公平的。至于开发人员,我们研究了与死锁预防相关的设计问题,甚至开发了一个可用于检测死锁的工具 

 简单的说:死锁就是会发生在两个或两个以上的thread在等待两个或两个以上的lock会被释放,且程序的环境却让lock永远无法释放(获释),这种情况下就会发生死锁,有意思的是即使没有涉及同步lock也有可能产生死锁现象,这种情况下死锁的状况涉及thread等待一些条件,这包括了等着要取得lock且同时也等着某变量进入特定的状态。

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/845091.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号