国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 學院 > 開發設計 > 正文

如何避免程序中的死鎖

2019-11-18 14:48:42
字體:
來源:轉載
供稿:網友

// : c13:DiningPhilosophers.java
// Demonstrates how deadlock can be hidden in a PRogram.
// {Args: 5 0 deadlock 4}
// From 'Thinking in Java, 3rd ed.' (c) BrUCe Eckel 2002
// www.BruceEckel.com. See copyright notice in CopyRight.txt.

import java.util.Random;
import java.util.Timer;
import java.util.TimerTask;

class Chopstick {
  private static int counter = 0;

  private int number = counter++;

  public String toString() {
    return "Chopstick " + number;
  }
}

class Philosopher extends Thread {
  private static Random rand = new Random();

  private static int counter = 0;

  private int number = counter++;

  private Chopstick leftChopstick;

  private Chopstick rightChopstick;

  static int ponder = 0; // Package access

  public Philosopher(Chopstick left, Chopstick right) {
    leftChopstick = left;
    rightChopstick = right;
    start();
  }

  public void think() {
    System.out.println(this + " thinking");
    if (ponder > 0)
      try {
        sleep(rand.nextInt(ponder));
      } catch (InterruptedException e) {
        throw new RuntimeException(e);
      }
  }

  public void eat() {
    synchronized (leftChopstick) {
      System.out.println(this + " has " + this.leftChopstick
          + " Waiting for " + this.rightChopstick);
      synchronized (rightChopstick) {
        System.out.println(this + " eating");
      }
    }
  }

  public String toString() {
    return "Philosopher " + number;
  }

  public void run() {
    while (true) {
      think();
      eat();
    }
  }
}

public class DiningPhilosophers {
  public static void main(String[] args) {
    if (args.length < 3) {
      System.err.println("usage:/n"
          + "java DiningPhilosophers numberOfPhilosophers "
          + "ponderFactor deadlock timeout/n"
          + "A nonzero ponderFactor will generate a random "
          + "sleep time during think()./n"
          + "If deadlock is not the string "
          + "'deadlock', the program will not deadlock./n"
          + "A nonzero timeout will stop the program after "
          + "that number of seconds.");
      System.exit(1);
    }
    Philosopher[] philosopher = new Philosopher[Integer.parseInt(args[0])];
    Philosopher.ponder = Integer.parseInt(args[1]);
    Chopstick left = new Chopstick(), right = new Chopstick(), first = left;
    int i = 0;
    while (i < philosopher.length - 1) {
      philosopher[i++] = new Philosopher(left, right);
      left = right;
      right = new Chopstick();
    }
    if (args[2].equals("deadlock"))
      philosopher[i] = new Philosopher(left, first);
    else
      // Swapping values prevents deadlock:
      philosopher[i] = new Philosopher(first, left);
    // Optionally break out of program:
    if (args.length >= 4) {
      int delay = Integer.parseInt(args[3]);
      if (delay != 0)
        new Timeout(delay * 1000, "Timed out");
    }
  }
} ///:~

class Timeout extends Timer {
  public Timeout(int delay, final String msg) {
    super(true); // Daemon thread
    schedule(new TimerTask() {
      public void run() {
        System.out.println(msg);
        System.exit(0);
      }
    }, delay);
  }
} ///:~


發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 龙胜| 松江区| 嘉鱼县| 长沙县| 阜康市| 盐源县| 长子县| 遂平县| 南宫市| 西平县| 周宁县| 应城市| 广水市| 定襄县| 楚雄市| 靖边县| 新竹县| 上高县| 诸暨市| 旬邑县| 光山县| 保德县| 济阳县| 鹿邑县| 南澳县| 临朐县| 吉首市| 芦溪县| 陈巴尔虎旗| 紫云| 台州市| 呼伦贝尔市| 福安市| 仁化县| 朝阳县| 汝州市| 措美县| 额济纳旗| 安丘市| 逊克县| 苏州市|