---
title: "附注 OF 《怎么写,程序才能死锁?》"
author: "keating"
date: "2011-04-05T17:03:12Z"
tags: []
categories: ["编程语言"]
slug: "of"
draft: false
lastmod: "2011-04-05T20:24:47Z"
id: 986731
url: "https://keating.iteye.com/blog/986731"
---

**这只是一篇<span style="color:red">附注</span>，请看博客文章 <span style="color:red">怎么写,程序才能死锁? <http://keating.iteye.com/blog/983365></span>**

**参考1，线程死锁**


||Thread1|Thread2|
|目的|获取i|获取j|
|步骤|获取i|获取j|
||等待j被释放,获取j|等待i被释放,获取i
||释放i|释放j|



```java

public class NewClass {

    boolean i = false;
    boolean j = false;

    public void begin() {
        Thread t1 = new Thread(new Thread1());
        Thread t2 = new Thread(new Thread2());
        t1.start();
        t2.start();
    }

    public static void main(String[] args) {
        new NewClass().begin();
    }

    class Thread1 implements Runnable {

        /**
         * 目的:获取j
         *
         * 步骤：
         * 获取i,
         * 等待j被释放,获取j
         * 释放i
         */
        public void run() {
            // 获取i
            i = true;

            // 休息一下,让另一个线程充分占有j
            try {
                Thread.sleep(1000);
            } catch (InterruptedException ex) {
                ex.printStackTrace();
            }

            // 等待j被释放,获取j
            while (j) {
            }
            j = true;

            // 释放i
            i = false;
        }
    }

    class Thread2 implements Runnable {

        /**
         * 目的:获取i
         *
         * 步骤：
         * 获取j,
         * 等待i被释放,获取i
         * 释放j
         */
        public void run() {
            // 获取j
            j = true;

            // 等待i被释放,获取i
            while (i) {
            }
            i = true;

            // 释放j
            j = false;
        }
    }
}

```


**参考2，DatabaseConnection类**


```

public class DatabaseConnection {

    private Connection con = null;
    private Statement stat = null;
    private PreparedStatement pstat = null;
    private ResultSet rs = null;

    public Connection con () {
        return con;
    }

    private void createConnection() throws Exception {
        try {
            try {
                Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver").newInstance();
            } catch (InstantiationException ex) {
                Logger.getLogger(DatabaseConnection.class.getName()).log(Level.SEVERE, null, ex);
            } catch (IllegalAccessException ex) {
                Logger.getLogger(DatabaseConnection.class.getName()).log(Level.SEVERE, null, ex);
            }
            StringBuilder sd = new StringBuilder("jdbc:sqlserver://");
            sd.append("localhost:1433;DatabaseName=");
            sd.append("FWDMS");//database name
            String url = sd.toString();
            DriverManager.setLoginTimeout(10);//设置连接超时为10秒
            con = DriverManager.getConnection(url, "sa", "sa");
        } catch (Exception ex) {
            throw new Exception(ex.getMessage());
        }
    }

    public static DatabaseConnection newInstance() throws Exception {
        DatabaseConnection dc = new DatabaseConnection();
        dc.createConnection();
        return dc;
    }

    public ResultSet query(String sql) {
        try {
            stat = con.createStatement();
            rs = stat.executeQuery(sql);
        } catch (Exception ex) {
            Logger.getLogger(DatabaseConnection.class.getName()).log(Level.SEVERE, null, ex);
        }
        return rs;
    }

    public void update(String sql) {
        try {
            stat = con.createStatement();
            stat.executeUpdate(sql);
        } catch (Exception ex) {
            Logger.getLogger(DatabaseConnection.class.getName()).log(Level.SEVERE, null, ex);
        }
    }

    public void update(String sql, String[] args) {
        try {
            pstat = con.prepareStatement(sql);
            for (int i = 0; i < args.length; i++) {
                pstat.setString(i + 1, args[i]);
            }
            pstat.executeUpdate();
        } catch (Exception ex) {
            Logger.getLogger(DatabaseConnection.class.getName()).log(Level.SEVERE, null, ex);
        }
    }

    public void close() {
        //check null,then close rs,stat,pstat and con.
        try {
            if (rs != null) {
                rs.close();
            }
            if (stat != null) {
                stat.close();
            }
            if (pstat != null) {
                pstat.close();
            }
            if (con != null) {
                con.close();
            }
        } catch (SQLException ex) {
            Logger.getLogger(DatabaseConnection.class.getName()).log(Level.SEVERE, null, ex);
        }
    }
}

```