国产探花免费观看_亚洲丰满少妇自慰呻吟_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);
  }
} ///:~


發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 秭归县| 象州县| 延庆县| 江都市| 浪卡子县| 锦屏县| 华容县| 平阳县| 丽水市| 正安县| 曲靖市| 宁夏| 浑源县| 福建省| 建瓯市| 南开区| 枣阳市| 阿拉尔市| 布尔津县| 通渭县| 安远县| 桃园市| 六安市| 扬州市| 林芝县| 西藏| 榕江县| 沂源县| 泰顺县| 基隆市| 临西县| 梓潼县| 游戏| 曲阜市| 新田县| 密山市| 九龙坡区| 田林县| 张家界市| 合阳县| 塘沽区|