📄 threadstests.java
字号:
//声明本类包含在包examples.jdbc.mssqlserver4中
package examples.jdbc.mssqlserver4;
//声明本类要引入的其他包和类
import java.io.*;
import java.sql.*;
import java.util.Properties;
/** 这个实例创建n个数据库连接,并同时运行n个线程。
*/
public class ThreadsTests
{
//数据库驱动
static String driverName = "weblogic.jdbc.mssqlserver4.Driver";
// 这个参数根据情况适当改变
static String url = "jdbc:weblogic:mssqlserver4";
// 不同的连接个数。
static int kNumberOfConnections = 3;
// 不同的线程共享同样的连接。不需要考虑同步的问题,因为驱动程序已处理了
static int kThreadsPerConnection = 4;
// 这个测试执行的时间
static int kTestSeconds = 60;
public static void main(String [] args)
{
// 设置用户名、密码和服务器名
Properties props = new Properties();
props.put("user", "sa");
props.put("password", "secret");
props.put("server", "pubs@myHost:1433");
Driver myDriver = null;
java.sql.Connection conn = null;
try
{
// 加载驱动程序
myDriver = (Driver) Class.forName(driverName).newInstance();
//声明线程数组
ReadingThread readers [] =
new ReadingThread[kNumberOfConnections * kThreadsPerConnection];
System.out.println("ThreadsTests - creating " +
(kNumberOfConnections * kThreadsPerConnection) +
" threads using " + kNumberOfConnections +
" connections to the server...\n");
` //建立连接
for (int i = 0 ; i < kNumberOfConnections ; i++)
{
// 连接到数据库服务器
conn = myDriver.connect(url, props);
// 创建使用同一连接的线程
for (int j = 0 ; j < kThreadsPerConnection ; j++)
{
readers[i * kThreadsPerConnection + j] = new ReadingThread(conn);
}
}
System.out.println("ThreadsTests - " +
(kNumberOfConnections * kThreadsPerConnection) +
" threads were created, starting them...\n");
// 启动我们创立的每个线程
for (int i = 0 ; i < readers.length ; i++)
{
if (readers[i] != null)
readers[i].start(); // Start threads
}
System.out.println("ThreadsTests - main thread is going to sleep so " +
"readers can torture the server...\n");
Thread.sleep(kTestSeconds * 1000); // 休眠一段时间,多线程在进行数据库操作
System.out.println("ThreadsTests - notifying readers that it's time to quit...\n");
for(int i = 0 ; i < readers.length ; i++) // 遍历每个线程
{
//通知每个线程停止运行
if(readers[i] != null) readers[i].running = false;
}
for(int i = 0 ; i < readers.length ; i++)
{
if(readers[i] != null && readers[i].isAlive())
{
try { readers[i].join(); }
catch(InterruptedException ie) { }
}
readers[i] = null;
}
System.out.println("\nThreadsTests - all readers stopped, we ran " +
totalRuns + " times (read " + totalRows +
" rows total).");
} catch(Exception sqlex) {
//异常处理
System.out.println("ThreadsTest - exception " + sqlex);
} finally { //close connections in a finally block
try {
if (conn != null)
//关闭连接
conn.close();
} catch (SQLException sqle) {
//异常处理
System.out.println("SQLException was thrown: " + sqle.getMessage());
}
}
System.out.println("\n\n(press Enter to exit)");
try { System.in.read(); } // wait for user to press a key before
// closing the console
// window
catch (IOException e) { return; }
}
static int totalRuns = 0; static int totalRows = 0;
synchronized static void updateStatistics(int runs, int rows)
{
totalRuns += runs;
totalRows += rows;
}
}
/** 线程创建类. */
class ReadingThread extends Thread
{
ReadingThread(Connection connection)
{
this.connection = connection;
}
Connection connection = null; // 这个线程的连接
boolean running = true; // 运行标记
public void run()
{
int runs = 0, rows = 0;
for( ; running ; runs++) // 保持反复查询
{
Statement statement = null;
try
{
// 创建查询语句对象
statement = connection.createStatement();
// 执行查询语句
ResultSet result =
statement.executeQuery("select * from publishers");
// 每行的列数
int i = 0, columns = result.getMetaData().getColumnCount();
// 浏览结果集的每行
for( ; result.next() && running ; i++)
{
// 每列
for(int j = 1 ; j <= columns ; j++)
{
// 取值
String s = result.getString(j);
try
{
Thread.sleep(25);
}
catch(InterruptedException ie) { }
}
}
System.out.println(" read " + i + " rows [" + this + "]...");
ThreadsTests.updateStatistics(1,i); rows += i;
}
catch(SQLException sqlEx)
{ System.out.println(sqlEx); }
if(statement != null)
{
try { statement.close(); }
catch (SQLException sqlEx)
{ System.out.println(sqlEx); }
}
}
connection = null;
System.out.println(toString() + " - ran the test " + runs
+ " times (total of " + rows + " rows read).");
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -