线程相关的

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();

    }
}

希望帮忙分析一下啊

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值