這篇文章給大家分享的是有關(guān)java線(xiàn)程池如何讀取單個(gè)SQL數(shù)據(jù)庫(kù)表的內(nèi)容。小編覺(jué)得挺實(shí)用的,因此分享給大家做個(gè)參考,一起跟隨小編過(guò)來(lái)看看吧。
成都創(chuàng)新互聯(lián)公司成立于2013年,我們提供高端重慶網(wǎng)站建設(shè)公司、成都網(wǎng)站制作公司、成都網(wǎng)站設(shè)計(jì)、網(wǎng)站定制、營(yíng)銷(xiāo)型網(wǎng)站建設(shè)、小程序設(shè)計(jì)、微信公眾號(hào)開(kāi)發(fā)、seo優(yōu)化服務(wù),提供專(zhuān)業(yè)營(yíng)銷(xiāo)思路、內(nèi)容策劃、視覺(jué)設(shè)計(jì)、程序開(kāi)發(fā)來(lái)完成項(xiàng)目落地,為成都茶樓設(shè)計(jì)企業(yè)提供源源不斷的流量和訂單咨詢(xún)。
任務(wù):基于線(xiàn)程池來(lái)操作MySQL,測(cè)試單臺(tái)機(jī)器讀寫(xiě)MySQL單表的效率。
思路:創(chuàng)建一個(gè)大小合適的線(xiàn)程池,讓每個(gè)線(xiàn)程分別連接到數(shù)據(jù)庫(kù)并進(jìn)行讀取輸出操作。
連接到數(shù)據(jù)庫(kù)
import java.sql.DriverManager; import java.sql.SQLException; import com.mysql.jdbc.Statement; public class TEXT { } class MySQLOpen { private Connection con = null; private static String driver = "com.mysql.jdbc.Driver"; private static String url = "jdbc:mysql://localhost:3306/phpmyadmin"; private static String username = "root"; private static String password = "root"; private static Statement NULL = null; public void MysqlOpen() { try { Class.forName(driver); //加載驅(qū)動(dòng)類(lèi) con = DriverManager.getConnection(url, username, password); //連接數(shù)據(jù)庫(kù) if (!con.isClosed()) System.out.println("***數(shù)據(jù)庫(kù)成功連接***"); } catch (ClassNotFoundException e) { System.out.println("找不到驅(qū)動(dòng)程序類(lèi),加載驅(qū)動(dòng)失敗"); e.printStackTrace(); } catch (SQLException e) { System.out.println("數(shù)據(jù)庫(kù)連接失敗"); e.printStackTrace(); } } }
利用statement類(lèi)中的executeQuery方法操作MySQL
Statement state = (Statement) con.createStatement(); ResultSet sql = state.executeQuery("select * from user where id between 1 and 5");
利用sql.next()循環(huán)遍歷取出想要的數(shù)據(jù)
while (sql.next()) { String id = sql.getString(1); String username = sql.getString(3); String text = sql.getString(6); System.out.println(id+"\t"+username+"\t"+text); }
以上就已經(jīng)實(shí)現(xiàn)了主線(xiàn)程訪(fǎng)問(wèn)并操作數(shù)據(jù)庫(kù)的相應(yīng)內(nèi)容。
創(chuàng)建線(xiàn)程池,設(shè)置好相應(yīng)參數(shù)
ThreadPoolExecutor executor = new ThreadPoolExecutor(5, 15, 200, TimeUnit.MILLISECONDS, new ArrayBlockingQueue<Runnable>(5));
利用for循環(huán)去創(chuàng)建線(xiàn)程即可。
計(jì)算效率
long start = System.currentTimeMillis(); long end = System.currentTimeMillis(); System.out.println("平均每秒可輸出: " + 100000 / (end - start) + " 條");
要注意主線(xiàn)程創(chuàng)建好其他線(xiàn)程后就繼續(xù)往下執(zhí)行了,所以要有一個(gè)判斷其他線(xiàn)程是否結(jié)束的語(yǔ)句
while (true) { if (executor.getActiveCount() == 0) break; }
可以利用Thread.activeCount()看一還有多少 活躍的線(xiàn)程。
System.out.println("activeCountMain1 : " + Thread.activeCount());
主要的思路就再上面,現(xiàn)在貼出整理好的代碼:
import java.sql.Connection; import java.sql.DriverManager; import java.sql.ResultSet; import java.sql.SQLException; import java.util.concurrent.ArrayBlockingQueue; import java.util.concurrent.ThreadPoolExecutor; import java.util.concurrent.TimeUnit; import com.mysql.jdbc.Statement; public class Main { public static void main(String[] args) { ThreadPoolExecutor executor = new ThreadPoolExecutor(5, 15, 200, TimeUnit.MILLISECONDS, new ArrayBlockingQueue<Runnable>(5)); long start = System.currentTimeMillis(); System.out.println("activeCountMain1 : " + Thread.activeCount()); for (int i = 1; i <= 20; i++) { MySQL mysql = new MySQL(i); executor.execute(mysql); System.out.println("線(xiàn)程池中線(xiàn)程數(shù)目:" + executor.getPoolSize() + ",隊(duì)列中等待執(zhí)行的任務(wù)數(shù)目:" + executor.getQueue().size() + ",已執(zhí)行玩別的任務(wù)數(shù)目:" + executor.getCompletedTaskCount()); } executor.shutdown(); while (true) { if (executor.getActiveCount() == 0) break; } System.out.println("activeCountMain2 : " + Thread.activeCount()); long end = System.currentTimeMillis(); System.out.println("平均每秒可輸出: " + 100000 / (end - start) + " 條"); } } class MySQL implements Runnable { private Connection con = null; private static String driver = "com.mysql.jdbc.Driver"; private static String url = "jdbc:mysql://localhost:3306/phpmyadmin"; private static String username = "root"; private static String password = "root"; private static Statement NULL = null; private final int taskNum; public MySQL(int taskNum) { this.taskNum = taskNum; } public Statement MysqlOpen() { try { Class.forName(driver); //加載驅(qū)動(dòng)類(lèi) con = DriverManager.getConnection(url, username, password); //連接數(shù)據(jù)庫(kù) if (!con.isClosed()) System.out.println("***數(shù)據(jù)庫(kù)成功連接***"); Statement state = (Statement) con.createStatement(); return state; } catch (ClassNotFoundException e) { System.out.println("找不到驅(qū)動(dòng)程序類(lèi),加載驅(qū)動(dòng)失敗"); e.printStackTrace(); } catch (SQLException e) { System.out.println("數(shù)據(jù)庫(kù)連接失敗"); e.printStackTrace(); } return NULL; } @Override public void run() { readMySQL(); } public void readMySQL() { ResultSet sql = null; Statement state = MysqlOpen(); try { sql = state.executeQuery("select * from sina_user_weibos_1386622641 where id between " + ((taskNum - 1) * 5000) + " and " + (taskNum * 5000)); System.out.println("---------task " + taskNum + "正在執(zhí)行---------"); while (sql.next()) { String id = sql.getString(1); String wid = sql.getString(2); String username = sql.getString(3); String repostscount = sql.getString(4); String commentscount = sql.getString(5); String text = sql.getString(6); String createat = sql.getString(7); String source = sql.getString(15); String lasttime = sql.getString(17); System.out.println(id + "\t" + wid + "\t" + username + "\t" + repostscount + "\t" + commentscount + "\t" + text + "\t" + createat + "\t" + source + "\t" + lasttime); } } catch (SQLException e) { e.printStackTrace(); } finally { try { sql.close(); state.close(); con.close(); } catch (Exception e) { e.printStackTrace(); } } System.out.println("---------task " + taskNum + "執(zhí)行完畢---------"); } }
感謝各位的閱讀!關(guān)于“java線(xiàn)程池如何讀取單個(gè)SQL數(shù)據(jù)庫(kù)表”這篇文章就分享到這里了,希望以上內(nèi)容可以對(duì)大家有一定的幫助,讓大家可以學(xué)到更多知識(shí),如果覺(jué)得文章不錯(cuò),可以把它分享出去讓更多的人看到吧!
本文名稱(chēng):java線(xiàn)程池如何讀取單個(gè)SQL數(shù)據(jù)庫(kù)表
轉(zhuǎn)載源于:http://aaarwkj.com/article0/gdiiio.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供電子商務(wù)、企業(yè)建站、響應(yīng)式網(wǎng)站、網(wǎng)站導(dǎo)航、網(wǎng)站策劃、小程序開(kāi)發(fā)
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶(hù)投稿、用戶(hù)轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請(qǐng)盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀(guān)點(diǎn)不代表本網(wǎng)站立場(chǎng),如需處理請(qǐng)聯(lián)系客服。電話(huà):028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來(lái)源: 創(chuàng)新互聯(lián)