线程相关的

来源:互联网 发布:食品安全事故数据统计 编辑:程序博客网 时间:2024/06/06 01:06

ConnectionPool.java

package pool;

import java.sql.Connection;

public interface ConnectionPool {

    Connection borrow();

    boolean repay(Connection conn);

}

ConnectionPoolImpl.java

package pool.impl;

import java.sql.Connection;

import pool.ConnectionPool;

public class ConnectionPoolImpl implements ConnectionPool {

    private static Object lock        = new Object();

    private int           borrowCount = 0;

    private int           max         = 3;

    private int           min         = 0;

    @Override
    public Connection borrow() {
        Connection conn = null;
        synchronized (lock) {
            if (borrowCount == max) {
                try {
                    System.out.println(Thread.currentThread().getId() + ",borrow wait");
                    lock.wait();
                } catch (InterruptedException e) {
                    e.printStackTrace();
                    lock.notify();
                }
            }
            conn = doBorrow();
            borrowCount++;
        }
        return conn;
    }

    private Connection doBorrow() {
        System.out.println(Thread.currentThread().getId() + ",doBorrow");
        return null;
    }

    @Override
    public boolean repay(Connection conn) {
        if (borrowCount == 0) {
            throw new IllegalStateException(Thread.currentThread().getId() + ",borrowCount = 0");
        }
        synchronized (lock) {
            boolean notify = (borrowCount == max);
            borrowCount--;
            doRepay(conn);
            if (notify) {
                lock.notify();
                System.out.println(Thread.currentThread().getId() + ",notify");
            }
        }
        return true;
    }


    private void doRepay(Connection conn) {
        System.out.println(Thread.currentThread().getId() + ",doRepay");
    }


    public int getMax() {
        return max;
    }

    public void setMax(int max) {
        this.max = max;
    }

    public int getMin() {
        return min;
    }

    public void setMin(int min) {
        this.min = min;
    }

}

Main.java

package test;

import pool.impl.ConnectionPoolImpl;

public class Main {

    static ConnectionPoolImpl c = new ConnectionPoolImpl();

    public static void main(String[] args) {

        Thread d = new Thread() {

            public void run() {
                while (true) {
                    try {
                        c.borrow();
                    } catch (Exception e) {
                        e.printStackTrace();
                    }
                    try {
                        System.out.println(Thread.currentThread().getId() + ",sleep");
                        Thread.sleep(300);
                    } catch (InterruptedException e1) {
                        e1.printStackTrace();
                    }
                }
            }
        };
        d.start();

        Thread f = new Thread() {

            public void run() {
                while (true) {
                    try {
                        System.out.println(Thread.currentThread().getId() + ",sleep");
                        Thread.sleep(1000);
                    } catch (InterruptedException e1) {
                        e1.printStackTrace();
                    }
                    try {
                        c.repay(null);
                    } catch (Exception e) {
                        e.printStackTrace();
                    }
                }
            }
        };
        f.start();

    }
}

希望帮忙分析一下啊