【问题标题】:Am I understanding the concept of resource allocation correctly?我是否正确理解资源分配的概念?
【发布时间】:2016-05-02 02:37:43
【问题描述】:

对于 Java 中同步如何进行资源分配,我有点困惑。 当我在 Java 中得到以下代码时:

import java.util.concurrent.Semaphore;

public class Deadlock {
public Deadlock () {
    Semaphore mutex[] = new Semaphore[4];

    for (int i = 0; i < 4; i++) 
        mutex[i] = new Semaphore(1);

    A threadA = new A(mutex);
    B threadB = new B(mutex);
    C threadC = new C(mutex);

    threadA.start();
    threadB.start();
    threadC.start();
}

private class A extends Thread {
    private Semaphore[] resource;

    public A(Semaphore[] m) {
        resource = m;
    }

    public void run() {
        System.out.println("A started");
        synchronized( resouce[1] ) {
            System.out.println("A locks rsc 1");
            synchronized (resource[0]) {
                System.out.println("A locks rsc 0");
            }
        }
        System.out.println("A finished");
    }
}
private class B extends Thread {
    private Semaphore[] resource;

    public B(Semaphore[] m) {
        resource = m;
    }

    public void run() {
        System.out.println("B started");
        synchronized( resouce[3] ) {
            System.out.println("B locks rsc 3");
            synchronized (resource[0]) {
                System.out.println("B locks rsc 0");
                synchronized (resource[2]) {
                    System.out.println("B locks rsc 2");
                }
            }
        }
        System.out.println("B finished");
    }
}
private class C extends Thread {
    private Semaphore[] resource;

    public C(Semaphore[] m) {
        resource = m;
    }

    public void run() {
        System.out.println("C started");
        synchronized( resouce[2] ) {
            System.out.println("C locks rsc 2");
            synchronized (resource[1]) {
                System.out.println("C locks rsc 1");
            }
        }
        System.out.println("C finished");
    }
}
}

据我了解,当线程 A 启动时,线程 A 锁定了资源 1 和资源 0。 因此,当线程 B 启动时,它会在资源 3 上获得锁,但会等待资源 0 从线程 A 中释放。由于线程 B 没有资源 0 上的锁,因此无法等待资源 2。 当线程 C 启动时,它会对资源 2 进行锁定,同时也在等待资源 1 从线程 A 中释放。

因此,当它绘制为资源分配图时,它将如下所示:

这里,从P到R的节点是指请求资源的进程。而从 R 到 P 的节点表示该进程已锁定资源。

我理解正确吗? 欢迎任何帮助。 谢谢。

【问题讨论】:

    标签: java process resources deadlock synchronized


    【解决方案1】:

    当线程 B 启动时,它将获取资源 3 上的锁,但将等待资源 0 从线程 A 中释放

    啊,但这假设A 仍在运行。它可能已经完成,也可能还没有开始。您遇到死锁条件的机会可能只发生在千分之一的运行中。这是它们异步、并发运行的线程的本质。很难预测它们的精确行为。

    此外,您还通过调用System.out.println(...) 来使事情复杂化,该System.out.println(...) 也在进行同步。这会中断您的测试并更改比赛条件。

    我理解正确吗?

    我认为您正确理解了资源图,而不是在正确的时间达到完美的死锁点有多难。

    要尝试的一件事是在 while(true) 循环中执行以下操作。

    while (true) {
       A threadA = new A(mutex);
       B threadB = new B(mutex);
       C threadC = new C(mutex);
       threadA.start();
       threadB.start();
       threadC.start();
       threadA.join();
       threadB.join();
       threadC.join();
    }
    

    在某个时刻,输出将停止,CPU 将变为 0,您将知道它已达到死锁。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2017-11-04
      • 2015-08-21
      • 1970-01-01
      • 1970-01-01
      • 2013-02-01
      • 2020-03-21
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多