public
class
DBPool
{
private
static
LinkedList<Connection> pool =
new
LinkedList
<Connection>();
public
DBPool
(
int
initialSize)
{
if
(initialSize >
0
) {
for
(
int
i
=
0
; i < initialSize; i++) {
pool.addLast(SqlConnectImpl.fetchConnection());
public
void
releaseConnection
(Connection connection)
{
if
(connection !=
null
) {
synchronized
(pool){
pool.addLast(connection);
pool.notifyAll();
public
Connection
fetchConnection
(
long
mills)
throws
InterruptedException {
synchronized
(pool){
if
(mills<
0
){
while
(pool.isEmpty()){
wait();
return
pool.removeFirst();
}
else
{
long
furture
=
System.currentTimeMillis()+mills;
long
remaining
=
mills;
while
(pool.isEmpty()&&remaining>
0
){
pool.wait(remaining);
remaining = furture-System.currentTimeMillis();
Connection
connection
=
null
;
if
(!pool.isEmpty()){
connection = pool.removeFirst();
return
connection;
DBTest.java
package cn.enjoyedu.ch1.pool;
import java.sql.Connection;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.atomic.AtomicInteger;
*类说明:
public class DBPoolTest {
static DBPool pool = new DBPool(20);
static CountDownLatch end;
public static void main(String[] args) throws Exception {
int threadCount = 50;
end = new CountDownLatch(threadCount);
int count = 20;
AtomicInteger got = new AtomicInteger();
AtomicInteger notGot = new AtomicInteger();
for (int i = 0; i < threadCount; i++) {
Thread thread = new Thread(new Worker(count, got, notGot),
"worker_"+i);
thread.start();
end.await();
System.out.println("总共尝试了: " + (threadCount * count));
System.out.println("拿到连接的次数: " + got);
System.out.println("没能连接的次数: " + notGot);
static class Worker implements Runnable {
int count;
AtomicInteger got;
AtomicInteger notGot;
public Worker(int count, AtomicInteger got,
AtomicInteger notGot) {
this.count = count;
this.got = got;
this.notGot = notGot;
public void run() {
while (count > 0) {
try {
Connection connection = pool.fetchConnection(1000);
if (connection != null) {
try {
connection.createStatement();
connection.commit();
} finally {
pool.releaseConnection(connection);
got.incrementAndGet();
} else {
notGot.incrementAndGet();
System.out.println(Thread.currentThread().getName()
+"等待超时!");
} catch (Exception ex) {
} finally {
count--;
end.countDown();
SqlConnectImpl.java 重写了大量的方法
package cn.enjoyedu.ch1.pool;
import cn.enjoyedu.tools.SleepTools;
import java.sql.*;
import java.util.Map;
import java.util.Properties;
import java.util.concurrent.Executor;
*类说明:
public class SqlConnectImpl implements Connection{
public static final Connection fetchConnection(){
return new SqlConnectImpl();
@Override
public boolean isWrapperFor(Class<?> arg0) throws SQLException {
return false;
@Override
public <T> T unwrap(Class<T> arg0) throws SQLException {
return null;
@Override
public void abort(Executor arg0) throws SQLException {
@Override
public void clearWarnings() throws SQLException {
@Override
public void close() throws SQLException {
@Override
public void commit() throws SQLException {
SleepTools.ms(70);
@Override
public Array createArrayOf(String arg0, Object[] arg1) throws SQLException {
return null;
@Override
public Blob createBlob() throws SQLException {
return null;
@Override
public Clob createClob() throws SQLException {
return null;
@Override
public NClob createNClob() throws SQLException {
return null;
@Override
public SQLXML createSQLXML() throws SQLException {
return null;
@Override
public Statement createStatement() throws SQLException {
SleepTools.ms(1);
return null;
@Override
public Statement createStatement(int arg0, int arg1) throws SQLException {
return null;
@Override
public Statement createStatement(int arg0, int arg1, int arg2) throws SQLException {
return null;
@Override
public Struct createStruct(String arg0, Object[] arg1) throws SQLException {
return null;
@Override
public boolean getAutoCommit() throws SQLException {
return false;
@Override
public String getCatalog() throws SQLException {
return null;
@Override
public Properties getClientInfo() throws SQLException {
return null;
@Override
public String getClientInfo(String arg0) throws SQLException {
return null;
@Override
public int getHoldability() throws SQLException {
return 0;
@Override
public DatabaseMetaData getMetaData() throws SQLException {
return null;
@Override
public int getNetworkTimeout() throws SQLException {
return 0;
@Override
public String getSchema() throws SQLException {
return null;
@Override
public int getTransactionIsolation() throws SQLException {
return 0;
@Override
public Map<String, Class<?>> getTypeMap() throws SQLException {
return null;
@Override
public SQLWarning getWarnings() throws SQLException {
return null;
@Override
public boolean isClosed() throws SQLException {
return false;
@Override
public boolean isReadOnly() throws SQLException {
return false;
@Override
public boolean isValid(int arg0) throws SQLException {
return false;
@Override
public String nativeSQL(String arg0) throws SQLException {
return null;
@Override
public CallableStatement prepareCall(String arg0) throws SQLException {
return null;
@Override
public CallableStatement prepareCall(String arg0, int arg1, int arg2) throws SQLException {
return null;
@Override
public CallableStatement prepareCall(String arg0, int arg1, int arg2, int arg3) throws SQLException {
return null;
@Override
public PreparedStatement prepareStatement(String arg0) throws SQLException {
return null;
@Override
public PreparedStatement prepareStatement(String arg0, int arg1) throws SQLException {
return null;
@Override
public PreparedStatement prepareStatement(String arg0, int[] arg1) throws SQLException {
return null;
@Override
public PreparedStatement prepareStatement(String arg0, String[] arg1) throws SQLException {
return null;
@Override
public PreparedStatement prepareStatement(String arg0, int arg1, int arg2) throws SQLException {
return null;
@Override
public PreparedStatement prepareStatement(String arg0, int arg1, int arg2, int arg3) throws SQLException {
return null;
@Override
public void releaseSavepoint(Savepoint arg0) throws SQLException {
@Override
public void rollback() throws SQLException {
@Override
public void rollback(Savepoint arg0) throws SQLException {
@Override
public void setAutoCommit(boolean arg0) throws SQLException {
@Override
public void setCatalog(String arg0) throws SQLException {
@Override
public void setClientInfo(Properties arg0) throws SQLClientInfoException {
@Override
public void setClientInfo(String arg0, String arg1) throws SQLClientInfoException {
@Override
public void setHoldability(int arg0) throws SQLException {
@Override
public void setNetworkTimeout(Executor arg0, int arg1) throws SQLException {
@Override
public void setReadOnly(boolean arg0) throws SQLException {
@Override
public Savepoint setSavepoint() throws SQLException {
return null;
@Override
public Savepoint setSavepoint(String arg0) throws SQLException {
return null;
@Override
public void setSchema(String arg0) throws SQLException {
@Override
public void setTransactionIsolation(int arg0) throws SQLException {
@Override
public void setTypeMap(Map<String, Class<?>> arg0) throws SQLException {
public static void main(String[] args) {
SleepLock sleepTest = new SleepLock();
Thread threadA = sleepTest.new ThreadSleep();
threadA.setName("ThreadSleep");
Thread threadB = sleepTest.new ThreadNotSleep();
threadB.setName("ThreadNotSleep");
threadA.start();
try {
Thread.sleep(1000);
System.out.println(" Main slept!");
} catch (InterruptedException e) {
e.printStackTrace();
threadB.start();
private class ThreadSleep extends Thread{
@Override
public void run() {
String threadName = Thread.currentThread().getName();
System.out.println(threadName+" will take the lock");
try {
synchronized(lock) {
System.out.println(threadName+" taking the lock");
Thread.sleep(5000);
System.out.println("Finish the work: "+threadName);
} catch (InterruptedException e) {
private class ThreadNotSleep extends Thread{
@Override
public void run() {
String threadName = Thread.currentThread().getName();
System.out.println(threadName+" will take the lock time="+System.currentTimeMillis());
synchronized(lock) {
System.out.println(threadName+" taking the lock time="+System.currentTimeMillis());
System.out.println("Finish the work: "+threadName);