设为首页 加入收藏

TOP

ThreadLocal,LinkedBlockingQueue,线程池 获取数据库连接2改进(一)
2015-07-20 17:57:29 来源: 作者: 【 】 浏览:2
Tags:ThreadLocal LinkedBlockingQueue 线程 获取 数据库 连接 改进
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
  
   > queueHoder = new ThreadLocal
   
    >(); 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
    
      getQueue() { LinkedBlockingQueue
     
       queue = queueHoder.get(); if (queue == null) { queue = new LinkedBlockingQueue
      
       (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
       
         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
首页 上一页 1 2 下一页 尾页 1/2/2
】【打印繁体】【投稿】【收藏】 【推荐】【举报】【评论】 【关闭】 【返回顶部
分享到: 
上一篇UVA - 590Always on the run(递.. 下一篇HDU 1863 畅通工程 (最小生成树)

评论

帐  号: 密码: (新用户注册)
验 证 码:
表  情:
内  容: