Java多线程死锁还是饥饿?

huangapple go评论57阅读模式
英文:

Java Multi-threading Deadlock or Starvation?

问题

我正在为我的OCP考试学习,试图理解何时会出现线程死锁,或者线程是否处于饥饿状态。在下面的场景中,我有疑问。

public class ThreadTest {

    private static int i = 0;

    public static void doSomething() {
        synchronized(ThreadTest.class) {
            while(true) {
                System.out.println("count:" + ++i);
            }
        }
    }

    public static void main(String args[]) { 
        new Thread(() -> doSomething()).start();
        new Thread(() -> doSomething()).start();
    }
}

第一个线程在doSomething()中获取了synchronized ThreadTest类的锁,并进入无限循环,永远不会释放锁。
第二个线程会一直等待,直到资源变为可用(但这永远不会发生)。

在这种情况下,我们在谈论死锁还是饥饿?我认为这是饥饿,因为一个线程无法访问共享资源,而死锁是指线程相互阻塞,无法继续执行。

死锁描述了线程永远被阻塞的情况。

饥饿描述了线程无法定期访问共享资源的情况。

英文:

I am learning for my OCP exam and are trying to understand when we are speaking of a thread deadlock or a thread is in starvation. in case of the scenario below I have doubt.

public class ThreadTest {

private static int i = 0;
    
public static void doSomething() {
synchronized(ThreadTest.class) {
    while(true) {
      System.out.println("count:" + ++i)
    } }
}

public static void main(String args[]) { 
New Thread(() -> doSomething()).start();
New Thread(() -> doSomething()).start();
}}

The first thread to acquires the lock of the synchronized ThreadTest class in doSomething() goes in a infinite loop never releasing the lock.
The second thread keep waiting til the resource becomes available (what never happens).

Are we speaking of a Deadlock in this situation or Starvation? I think about Starvation because one thread is not getting access to a shared resource and for a deadlock threads block each others resources. But just to be sure i ask about it here.

Deadlock describes a situation where threads are blocked forever.

Starvation describes a situation where a thread is unable to gain regular access to shared resources.

答案1

得分: 0

正如评论中指出的那样。死锁发生在以下4个条件同时满足时。它们是:

  1. 互斥 - B想要A拥有的资源。B不能使用A正在使用的资源(即此处的情况)。
  2. 持有和等待 - A持有B想要的资源。A还在等待其他人拥有的资源被释放(根据问题陈述,A并未出现这种情况)。
  3. 不可抢占 - A持有的资源不能被强行夺走(假设在这里成立,因为未有其他说明)。
  4. 循环等待 - A持有B想要的资源。B持有A想要的资源(在这里没有发生)。

因此我们可以看到,不满足所有条件。因此在这种情况下不能发生死锁。

然而,由于一个无限循环,A永远不会释放锁。因此B将被饿死。

这应该能解决你的疑惑。干杯。

英文:

As pointed out in comment. Deadlock happens when the below 4 conditions happens. Them being :

  1. Mutual Exclusion - B wants a resource A has. B cannot use the
    resource A is using(which is happening here).
  2. Hold and wait - A is holding a resource that B wants. A is also waiting for a resource to be freed that somebody else has(Not happening for A from the probem statement given)
  3. No Preemption - The resource A is holding cannot be snatched away from it by force(assuming that it is true here because not given other wise).
  4. Circular Wait - A is holding a resouce B wants. B is holding a resource A wants(Not happening here).

Hence we see here that all the conditions are not met. So a deadlock cannot exist in this situation.

There is however an infinite loop due to which A never gives up the lock. Hence B will be starved.

This should clear your doubt. Cheers

huangapple
  • 本文由 发表于 2020年9月11日 16:01:54
  • 转载请务必保留本文链接:https://go.coder-hub.com/63843086.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定