package com.ctl.util;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.util.Random;
import java.util.concurrent.LinkedBlockingQueue;
import java.util.concurrent.TimeUnit; import com.ctl.util.ConfigUtils;
import com.ctl.util.LogUtil; public class ThreadLocalBlockingQueueUtils {
private static ThreadLocal<LinkedBlockingQueue<Connection>> queueHoder = new ThreadLocal<LinkedBlockingQueue<Connection>>();
public static int num = 0;
private static String driver;
private static String url;
private static String username;
private static String password;
private static int threadPoolMaxNum; private static int threadPoolMinNum;
private static LogUtil log;
// \u6570\u636E\u5E93\u7C7B\u578Boracle mysql db2
private static String databasetype;
public static int getThreadPoolMaxNum() {
return threadPoolMaxNum;
} public static int getThreadPoolMinNum() {
return threadPoolMinNum;
} public static String getUrl() {
return url;
} public static String getUsername() {
return username;
} public static String getPassword() {
return password;
} static {
log = new LogUtil();
databasetype = ConfigUtils.getType("databasetype");
threadPoolMaxNum = Integer.parseInt(ConfigUtils
.getType("threadPoolMaxNum"));
threadPoolMinNum = Integer.parseInt(ConfigUtils
.getType("threadPoolMinNum"));
if (databasetype.equals("mysql")) {
driver = ConfigUtils.getType("mysql.driver");
url = ConfigUtils.getType("mysql.url");
username = ConfigUtils.getType("mysql.username");
password = ConfigUtils.getType("mysql.password");
} else if (databasetype.equals("oracle")) {
driver = ConfigUtils.getType("oracle.driver");
url = ConfigUtils.getType("oracle.url");
username = ConfigUtils.getType("oracle.username");
password = ConfigUtils.getType("oracle.password");
}
try {
Class.forName(driver);
} catch (ClassNotFoundException e) {
System.out.println(e.getMessage());
}
CreateConnection createConn = new CreateConnection();
createConn.setDaemon(true);
createConn.start();
} public static synchronized LinkedBlockingQueue<Connection> getQueue() {
LinkedBlockingQueue<Connection> queue = queueHoder.get();
if (queue == null) {
queue = new LinkedBlockingQueue<Connection>(threadPoolMaxNum);
queueHoder.set(queue);
return queue;
}
return queue;
} // static void start() {
// Tom tom = new Tom();
// tom.start();
// for(;;){
// try {
// Thread.sleep(200);
// System.out.println("/*************"+getConnection()+"*************/");
// } catch (InterruptedException e) {
// }
// }
// }
//
// public static void main(String[] args) {
// ThreadLocalBlockingQueueUtils.start();
// } public static Connection getConnection() {
// System.out.println("进入getConnection");
class GetConnectionClazz extends Thread {
LinkedBlockingQueue<Connection> queue = ThreadLocalBlockingQueueUtils
.getQueue();
private Connection conn;
private int queueSize;
public int getQueueSize() {
return queueSize;
}
public Connection getConn() {
return conn;
}
public synchronized void run() {
// System.out.println("进入getConnection run()");
try {
// System.err.println("-----"+conn+"--------");
while (conn == null) {// 非常重要没有该while循环当按F5不断刷新时,仅仅要有一个取出来为空后面的全为空
conn = queue.poll(2, TimeUnit.SECONDS);
}
queueSize=queue.size();
// System.err.println("*******"+conn+"*********");
// if (conn != null) {
// System.err.println("\u3010" + queue.size() + "\u3011"
// + "getConnecion\u6210\u529F\uFF1A" + conn);
// }
} catch (InterruptedException e) {
log.WriteLine("getConnection", e.getMessage());
}
}
}
GetConnectionClazz jj = new GetConnectionClazz();
jj.start();
try {
jj.join();
} catch (InterruptedException e) {
log.WriteLine("getConnection()", e.getMessage());
}
log.WriteLine("getConnection()","\u3010" + jj.getQueueSize() + "\u3011"+ "getConnecion\u6210\u529F\uFF1A" + jj.getConn());
return jj.getConn();
} } // class GG extends Thread {
// public void run() {
// for (;;){
// try {
// ThreadLocalBlockingQueueUtils.getConnection();
// Thread.sleep(300);
// } catch (Exception e) {
// }
// }
// }
// }
/**
* @descritpion 创建数据库连接的线程类
* @author Administrator
*
*/
class CreateConnection extends Thread {
LinkedBlockingQueue<Connection> queue = ThreadLocalBlockingQueueUtils
.getQueue();
LogUtil log = new LogUtil(); public synchronized void run() {
boolean result = false;
while (true) {
try {
Random rand=new Random();
int num=ThreadLocalBlockingQueueUtils.getThreadPoolMaxNum()-ThreadLocalBlockingQueueUtils.getThreadPoolMinNum();
int randSize=rand.nextInt(num)+1;
if(queue.size()>=randSize){
Thread.sleep(100);
continue;
}
Connection conn = null;
conn = DriverManager.getConnection(
ThreadLocalBlockingQueueUtils.getUrl(),
ThreadLocalBlockingQueueUtils.getUsername(),
ThreadLocalBlockingQueueUtils.getPassword());
if (conn != null) {
result = queue.offer(conn, 1, TimeUnit.SECONDS);
} else {
// System.out.println("DriverManager.getConnection is null");
log.WriteLine("CreateConnection.run()",
"DriverManager.getConnection()返回 null");
continue;
}
if (result == false) {
Thread.sleep(100);
log.WriteLine("CreateConnection.run()",
"已达到最大连接数queue.size()=" + queue.size());
// System.out.println("已经满了size=【" + queue.size() + "】");
} else {
log.WriteLine("CreateConnection.run()",
"\u3010" + queue.size() + "\u3011"
+ "createConnection success:" + conn);
// System.out.println("\u3010" + queue.size() + "\u3011"
// + "createConnection success:" + conn);
}
} catch (InterruptedException e) {
// e.printStackTrace();
log.WriteLine("getConnection", e.getMessage());
// System.err.println(e.getMessage());
} catch (SQLException e) {
log.WriteLine("getConnection", e.getMessage());
// e.printStackTrace();
// System.err.println(e.getMessage());
}
}
}
}

ThreadLocal,LinkedBlockingQueue,线程池 获取数据库连接2改进的更多相关文章

  1. ThreadLocal与线程池使用的问题

    感谢博主的这篇分享,见 https://www.cnblogs.com/qifenghao/p/8977378.html 在今天的面试中,突然被考官问了这个问题,当时脱口而出的是 threadloca ...

  2. dbcp2连接池获取数据库连接Connection

    一.先来看看手工创建的方式 public static Connection getConnection() { Connection conn = null; try { Class.forName ...

  3. 当ThreadLocal碰上线程池

    ThreadLocal使用 ThreadLocal可以让线程拥有本地变量,在web环境中,为了方便代码解耦,我们通常用它来保存上下文信息,然后用一个util类提供访问入口,从controller层到s ...

  4. 源码分析—ThreadPoolExecutor线程池三大问题及改进方案

    前言 在一次聚会中,我和一个腾讯大佬聊起了池化技术,提及到java的线程池实现问题,我说这个我懂啊,然后巴拉巴拉说了一大堆,然后腾讯大佬问我说,那你知道线程池有什么缺陷吗?我顿时哑口无言,甘拜下风,所 ...

  5. ThreadLocal遇到线程池时, 各线程间的数据会互相干扰, 串来串去

    最近遇到一个比较隐蔽而又简单地问题,在使用ThreadLocal时发现出现多个线程中值串来串去,排查一番,确定问题为线程池的问题,线程池中的线程是会重复利用的,而ThreadLocal是用线程来做Ke ...

  6. python并发——从线程池获取返回值

    并发是快速处理大量相似任务的绝佳办法,但对于有返回值的方法,需要一个容器专门来存储每个进程处理完的结果 from multiprocessing import Pool import time #返回 ...

  7. 线程池(Java中有哪些方法获取多线程)

    线程池(Java中有哪些方法获取多线程) 前言 获取多线程的方法,我们都知道有三种,还有一种是实现Callable接口 实现Runnable接口 实现Callable接口 实例化Thread类 使用线 ...

  8. mysql 线程池 数据库连接池

    当客户端请求的数据量比较大的时候,使用线程池可以节约大量的系统资源,使得更多的CPU时间和内存可以高效地利用起来.而数据库连接池的使用则将大大提高程序运行效率,同时,我们可以通过其自身的管理机制来监视 ...

  9. Java 中的几种线程池,你之前用对了吗

    好久不发文章了,难道是因为忙,其实是因为懒.这是一篇关于线程池使用和基本原理的科普水文,如果你经常用到线程池,不知道你的用法标准不标准,是否有隐藏的 OOM 风险.不经常用线程池的同学,还有对几种线程 ...

随机推荐

  1. jsonp 格式

    jQuery(document).ready(function(){ $.ajax({ type: "get", async: false, url: "http://f ...

  2. 配置 L3 agent

    上一节我们介绍了路由服务(Routing)的基本功能,今天教大家如何配置. Neutron 的路由服务是由 l3 agent 提供的. 除此之外,l3 agent 通过 iptables 提供 fir ...

  3. pcm2aac

    1.下载faac源代码:http://downloads.sourceforge.net/faac/faac-1.28.zip 2.在VAWARE上进行交叉编译,安装. ./configure --t ...

  4. mysql中PDO参数化引号引起的Warning: PDOStatement::execute(): SQLSTATE[HY093]:报错

    sql语句之前是这样写的:INSERT INTO tablename SET a = ':a',b = ':b',c = ':c',d = :d,e = :e $data = array ( [':a ...

  5. HDU4757 Tree(可持久化Trie)

    写过可持久化线段树,但是从来没写过可持久化的Trie,今天补一补. 题目就是典型的给你一个数x,和一个数集,问x和里面的某个数xor起来的最大值是多少. 最原始的是数集是固定的,只需要对数集按照高到低 ...

  6. POJ 2492 A Bug's Life 并查集的应用

    题意:有n只虫子,每次给出一对互为异性的虫子的编号,输出是否存在冲突. 思路:用并查集,每次输入一对虫子后就先判定一下.如果两者父亲相同,则说明关系已确定,再看性别是否相同,如果相同则有冲突.否则就将 ...

  7. dump_stack的简单使用

    转载:http://blog.csdn.net/sanchuyayun/article/details/39183941 刚刚接触内核,在调试过程中用printk打印信息当然是直接有效的办法,但当我们 ...

  8. Facebook Rebound 弹性动画库 源码分析

    Rebound源码分析 让动画不再僵硬:Facebook Rebound Android动画库介绍一文中介绍了rebound这个库. 对于想体验一下rebound的效果,又懒得clone和编译代码的, ...

  9. MFC中 CDateTimeCtrl 自定义日期显示格式

    MFC里的DateTimePicker控件 ,通过属性来设置的话只能设置两种显示方式,要么日期,要么时间,很多时候我们需要在一个DateTimePicker里日期和时间同时显示. 这个时候只能通过自定 ...

  10. 2017.2.28 activiti实战--第七章--Spring容器集成应用实例(五)普通表单

    学习资料:<Activiti实战> 第七章  Spring容器集成应用实例(五)普通表单 第六章中介绍了动态表单.外置表单.这里讲解第三种表单:普通表单. 普通表单的特点: 把表单内容写在 ...