线程学习(28)-连接池

分析一下,最简单的连接池要有什么?

属性的话需要有连接数,以及连接数组,以及连接状态(代表当前连接是否被用)。以及一个全局状态(保证线程安全)。

方法的话,获取连接(多线程),以及释放连接(单线程)。

以及相关的构造函数。

package com.bo.threadstudy.seven;

import lombok.extern.slf4j.Slf4j;

import java.sql.*;
import java.util.Map;
import java.util.Properties;
import java.util.concurrent.Executor;
import java.util.concurrent.atomic.AtomicIntegerArray;

/**
 * 定义一个连接池,这个连接池需要有有一个线程安全的队列来维护,在多线程有存放和取连接的过程
 * 而且得标记哪个连接被取了,被取的连接是不能被拿走的
 * 属性,Conntion数组,AtomicIntegerArray(每个连接的状态,我最开始想的是Conntion作为Atomic,虽然性,但是没有状态),连接数
 * 定义的方法,取连接,放连接
 */
@Slf4j
public class ConnectionPoolTest {
    public static void main(String[] args) {
        ConnectionPool connectionPool = new ConnectionPool(1);
        new Thread(() -> {
            Connection connection = connectionPool.getConnection();
            connectionPool.releaseConnection(connection);
        }).start();

        new Thread(() -> {
            Connection connection = connectionPool.getConnection();
            connectionPool.releaseConnection(connection);
        }).start();

    }

}

@Slf4j
class ConnectionPool {
    //连接对象
    private Connection[] connections;
    //连接池的状态,0代表未使用,1代表已使用
    private AtomicIntegerArray statusList;
    //连接池的数量
    private Integer poolSize;

    private final int unusedStatus = 0;

    private final int usedStatus = 1;

    //根据happen-before原则中的lock原则,不需要加volatile,可以保证可见
    private int globalStatus = 0;

    //创建对象这个过程肯定是单线程操作
    ConnectionPool(int poolSize) {
        this.poolSize = poolSize;
        statusList = new AtomicIntegerArray(poolSize);
        connections = new Connection[poolSize];
        for (int i = 0; i < poolSize; i++) {
            connections[i] = new MockConnection();
            statusList.set(i, unusedStatus);
        }
    }

    //获取连接,这里可能涉及到多线程操作
    public Connection getConnection() {
        //while(true)得放到上面,得保证如果获取不到连接阻塞继续获取
        while (true) {
            for (Integer i = 0; i < poolSize; i++) {
                int curStatus = statusList.get(i);
                if (curStatus == unusedStatus) {
                    //取出连接
                    if (statusList.compareAndSet(i, curStatus, usedStatus)) {
                        log.debug("获取连接成功");
                        return connections[i];
                    }
                }
            }
            //第一个轮询执行完成后,会存在一个问题,假设执行到这一步后,指令交错,切换到唤醒的程序,先唤醒了,然后这里wait,就阻塞了,出现问题
            synchronized (this){
                try {
                    if(globalStatus == unusedStatus){
                        //代表此时有连接可用,不需要阻塞,继续往下走
                        log.debug("发生指令交错,连接可用");
                    }else{
                        log.debug("线程等待");
                        globalStatus = usedStatus;
                        this.wait();
                    }

                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }

        }
    }

    //连接释放,仅存在于单线程中,并且释放连接时,连接的比较可以用==来判定地址
    public void releaseConnection(Connection connection) {
        for (Integer i = 0; i < poolSize; i++) {
            if (connection == connections[i]) {
                //地址相同
                statusList.set(i,unusedStatus);
                //释放连接后,唤醒等待连接的线程
                synchronized (this){
                    globalStatus = unusedStatus;
                    this.notifyAll();
                    log.debug("释放连接,连接唤醒");
                }
                break;
            }
        }
    }


}


class MockConnection implements Connection {

    @Override
    public Statement createStatement() throws SQLException {
        return null;
    }

    @Override
    public PreparedStatement prepareStatement(String sql) throws SQLException {
        return null;
    }

    @Override
    public CallableStatement prepareCall(String sql) throws SQLException {
        return null;
    }

    @Override
    public String nativeSQL(String sql) throws SQLException {
        return null;
    }

    @Override
    public void setAutoCommit(boolean autoCommit) throws SQLException {

    }

    @Override
    public boolean getAutoCommit() throws SQLException {
        return false;
    }

    @Override
    public void commit() throws SQLException {

    }

    @Override
    public void rollback() throws SQLException {

    }

    @Override
    public void close() throws SQLException {

    }

    @Override
    public boolean isClosed() throws SQLException {
        return false;
    }

    @Override
    public DatabaseMetaData getMetaData() throws SQLException {
        return null;
    }

    @Override
    public void setReadOnly(boolean readOnly) throws SQLException {

    }

    @Override
    public boolean isReadOnly() throws SQLException {
        return false;
    }

    @Override
    public void setCatalog(String catalog) throws SQLException {

    }

    @Override
    public String getCatalog() throws SQLException {
        return null;
    }

    @Override
    public void setTransactionIsolation(int level) throws SQLException {

    }

    @Override
    public int getTransactionIsolation() throws SQLException {
        return 0;
    }

    @Override
    public SQLWarning getWarnings() throws SQLException {
        return null;
    }

    @Override
    public void clearWarnings() throws SQLException {

    }

    @Override
    public Statement createStatement(int resultSetType, int resultSetConcurrency) throws SQLException {
        return null;
    }

    @Override
    public PreparedStatement prepareStatement(String sql, int resultSetType, int resultSetConcurrency) throws SQLException {
        return null;
    }

    @Override
    public CallableStatement prepareCall(String sql, int resultSetType, int resultSetConcurrency) throws SQLException {
        return null;
    }

    @Override
    public Map<String, Class<?>> getTypeMap() throws SQLException {
        return null;
    }

    @Override
    public void setTypeMap(Map<String, Class<?>> map) throws SQLException {

    }

    @Override
    public void setHoldability(int holdability) throws SQLException {

    }

    @Override
    public int getHoldability() throws SQLException {
        return 0;
    }

    @Override
    public Savepoint setSavepoint() throws SQLException {
        return null;
    }

    @Override
    public Savepoint setSavepoint(String name) throws SQLException {
        return null;
    }

    @Override
    public void rollback(Savepoint savepoint) throws SQLException {

    }

    @Override
    public void releaseSavepoint(Savepoint savepoint) throws SQLException {

    }

    @Override
    public Statement createStatement(int resultSetType, int resultSetConcurrency, int resultSetHoldability) throws SQLException {
        return null;
    }

    @Override
    public PreparedStatement prepareStatement(String sql, int resultSetType, int resultSetConcurrency, int resultSetHoldability) throws SQLException {
        return null;
    }

    @Override
    public CallableStatement prepareCall(String sql, int resultSetType, int resultSetConcurrency, int resultSetHoldability) throws SQLException {
        return null;
    }

    @Override
    public PreparedStatement prepareStatement(String sql, int autoGeneratedKeys) throws SQLException {
        return null;
    }

    @Override
    public PreparedStatement prepareStatement(String sql, int[] columnIndexes) throws SQLException {
        return null;
    }

    @Override
    public PreparedStatement prepareStatement(String sql, String[] columnNames) throws SQLException {
        return null;
    }

    @Override
    public Clob createClob() throws SQLException {
        return null;
    }

    @Override
    public Blob createBlob() throws SQLException {
        return null;
    }

    @Override
    public NClob createNClob() throws SQLException {
        return null;
    }

    @Override
    public SQLXML createSQLXML() throws SQLException {
        return null;
    }

    @Override
    public boolean isValid(int timeout) throws SQLException {
        return false;
    }

    @Override
    public void setClientInfo(String name, String value) throws SQLClientInfoException {

    }

    @Override
    public void setClientInfo(Properties properties) throws SQLClientInfoException {

    }

    @Override
    public String getClientInfo(String name) throws SQLException {
        return null;
    }

    @Override
    public Properties getClientInfo() throws SQLException {
        return null;
    }

    @Override
    public Array createArrayOf(String typeName, Object[] elements) throws SQLException {
        return null;
    }

    @Override
    public Struct createStruct(String typeName, Object[] attributes) throws SQLException {
        return null;
    }

    @Override
    public void setSchema(String schema) throws SQLException {

    }

    @Override
    public String getSchema() throws SQLException {
        return null;
    }

    @Override
    public void abort(Executor executor) throws SQLException {

    }

    @Override
    public void setNetworkTimeout(Executor executor, int milliseconds) throws SQLException {

    }

    @Override
    public int getNetworkTimeout() throws SQLException {
        return 0;
    }

    @Override
    public <T> T unwrap(Class<T> iface) throws SQLException {
        return null;
    }

    @Override
    public boolean isWrapperFor(Class<?> iface) throws SQLException {
        return false;
    }
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值