ThreadLocal, javasblockingqueue, thread pool get database connection 2 improvement, thread pool
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 Thre AdLocal <shortblockingqueue <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 db2private static String databasetype; public static int getThreadPool MaxNum () {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 ("threadPoolMaxN Um "); 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 = Confi GUtils. 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 into blockingqueue <Connection> getQueue (){ Required blockingqueue <Connection> queue = queueHoder. get (); if (queue = null) {queue = new LinkedBlockingQueue <Connection> (threadPoolMaxNum); queueHoder. set (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 ("Enter getConnection"); class GetConnectionClazz extends Thread {define blockingqueue <Connection> queue = ThreadLocalBlockingQueueUtils. getQueue (); private Connection conn; private int queueSize; public int getQueueSize () {return queueSize;} p Ublic Connection getConn () {return conn;} public synchronized void run () {// System. out. println ("Enter getConnection run ()"); try {// System. err. println ("-----" + conn + "--------"); while (conn = null) {// It is important that the while loop is not refreshed when you press F5, as long as one of them is null, all of them are empty. 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 the Thread class for creating a database connection * @ 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 () returns null "); continue;} if (result = false) {Thread. sleep (100); log. writeLine ("CreateConnection. run () "," the maximum number of connections has been reached. size () = "+ queue. size (); // System. out. println ("already full 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 ());}}}}