基于java線程池讀取單個SQL數(shù)據(jù)庫表
任務(wù):基于線程池來操作MySQL,測試單臺機(jī)器讀寫MySQL單表的效率。
思路:創(chuàng)建一個大小合適的線程池,讓每個線程分別連接到數(shù)據(jù)庫并進(jìn)行讀取輸出操作。
連接到數(shù)據(jù)庫
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ū)動類
con = DriverManager.getConnection(url, username, password); //連接數(shù)據(jù)庫
if (!con.isClosed())
System.out.println("***數(shù)據(jù)庫成功連接***");
} catch (ClassNotFoundException e) {
System.out.println("找不到驅(qū)動程序類,加載驅(qū)動失敗");
e.printStackTrace();
} catch (SQLException e) {
System.out.println("數(shù)據(jù)庫連接失敗");
e.printStackTrace();
}
}
}
利用statement類中的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)實現(xiàn)了主線程訪問并操作數(shù)據(jù)庫的相應(yīng)內(nèi)容。
創(chuàng)建線程池,設(shè)置好相應(yīng)參數(shù)
ThreadPoolExecutor executor = new ThreadPoolExecutor(5, 15, 200, TimeUnit.MILLISECONDS, new ArrayBlockingQueue<Runnable>(5));
利用for循環(huán)去創(chuàng)建線程即可。
計算效率
long start = System.currentTimeMillis();
long end = System.currentTimeMillis();
System.out.println("平均每秒可輸出: " + 100000 / (end - start) + " 條");
要注意主線程創(chuàng)建好其他線程后就繼續(xù)往下執(zhí)行了,所以要有一個判斷其他線程是否結(jié)束的語句
while (true) {
if (executor.getActiveCount() == 0)
break;
}
可以利用Thread.activeCount()看一還有多少 活躍的線程。
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("線程池中線程數(shù)目:" + executor.getPoolSize() + ",隊列中等待執(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ū)動類
con = DriverManager.getConnection(url, username, password); //連接數(shù)據(jù)庫
if (!con.isClosed())
System.out.println("***數(shù)據(jù)庫成功連接***");
Statement state = (Statement) con.createStatement();
return state;
} catch (ClassNotFoundException e) {
System.out.println("找不到驅(qū)動程序類,加載驅(qū)動失敗");
e.printStackTrace();
} catch (SQLException e) {
System.out.println("數(shù)據(jù)庫連接失敗");
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í)行完畢---------");
}
}
以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
如何獲取springboot打成jar后的classpath
這篇文章主要介紹了如何獲取springboot打成jar后的classpath問題,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2023-07-07
Java中HashMap和Hashtable及HashSet的區(qū)別
以下是對Java中HashMap和Hashtable及HashSet的區(qū)別進(jìn)行了詳細(xì)的分析介紹,需要的朋友可以過來參考下2013-09-09
Maven設(shè)置JDK版本的兩種方法實現(xiàn)
本文主要介紹了Maven設(shè)置JDK版本的兩種方法實現(xiàn),是通過Apache Maven Compiler Plugin插件實現(xiàn)的,具有一定的參考價值,感興趣的可以了解一下2024-07-07
springboot實現(xiàn)敏感字段加密存儲解密顯示功能
這篇文章主要介紹了springboot實現(xiàn)敏感字段加密存儲,解密顯示,通過mybatis,自定義注解+AOP切面,Base64加解密方式實現(xiàn)功能,本文通過代碼實現(xiàn)給大家介紹的非常詳細(xì),需要的朋友可以參考下2022-02-02
Java開發(fā)之內(nèi)部類對象的創(chuàng)建及hook機(jī)制分析
這篇文章主要介紹了Java開發(fā)之內(nèi)部類對象的創(chuàng)建及hook機(jī)制,結(jié)合實例形式分析了java基于hook機(jī)制內(nèi)部類對象的創(chuàng)建與使用,需要的朋友可以參考下2018-01-01
SpringCloud @RefreshScope刷新機(jī)制深入探究
RefeshScope這個注解想必大家都用過,在微服務(wù)配置中心的場景下經(jīng)常出現(xiàn),他可以用來刷新Bean中的屬性配置,那大家對他的實現(xiàn)原理了解嗎?它為什么可以做到動態(tài)刷新呢2023-03-03

