网络编程 | 站长之家 | 网页制作 | 图形图象 | 操作系统 | 冲浪宝典 | 软件教学 | 网络办公 | 邮件系统 | 网络安全 | 认证考试 | 系统进程
Firefox | IE | Maxthon | 迅雷 | 电驴 | BitComet | FlashGet | QQ | QQ空间 | Vista | 输入法 | Ghost | Word | Excel | wps | Powerpoint
asp | .net | php | jsp | Sql | c# | Ajax | xml | Dreamweaver | FrontPages | Javascript | css | photoshop | fireworks | Flash | Cad | Discuz!
当前位置 > 网站建设学院 > 网络编程 > Java
Tag:注入,存储过程,分页,安全,优化,xmlhttp,fso,jmail,application,session,防盗链,stream,无组件,组件,md5,乱码,缓存,加密,验证码,算法,cookies,ubb,正则表达式,水印,索引,日志,压缩,base64,url重写,上传,控件,Web.config,JDBC,函数,内存,PDF,迁移,结构,破解,编译,配置,进程,分词,IIS,Apache,Tomcat,phpmyadmin,Gzip,触发器,socket
网络编程:ASP教程,ASP.NET教程,PHP教程,JSP教程,C#教程,数据库,XML教程,Ajax,Java,Perl,Shell,VB教程,Delphi,C/C++教程,软件工程,J2EE/J2ME,移动开发
本月文章推荐
.理解JTWI的具体内容和作用.
.Java的多线程-实现多线程及线程的.
.Java应用:编写高级JavaScript应.
.用 Java 实现回调例程.
.Java语言基础 一.
.使用动态代理实现用AOP对数据库进.
.实现Java与C语言接口步骤.
.在中文Windows95环境下用Java设计.
.J2SE5.0新特性之使用代理服务器.
.JAVA入门(1) 什么是JAVA.
.决定实施方案.
.Java 2 Micro Edition简介(一).
.如何使用AOP编程减少升级的风险(.
.Eclipse+Jboss使用小问题总结.
.Java常见问题集锦25问.
.用JAXB生成一个XML文档.
.系统构建高性能J2EE应用的五种核.
.Eclipse入门之使用指南及开发Ecl.
.JavaApplet编程之实现显示图像.
.J2SE综合:对java.util的总结 九.

如何避免程序中的死锁

发表日期:2008-1-5



// : 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);
  }
} ///:~

上一篇:Serializable对象序列化实例 人气:1143
下一篇:一个死锁的例子 人气:968
浏览全部Java的内容 Dreamweaver插件下载 网页广告代码 祝你圣诞节快乐 2009年新年快乐