昨天做了個HashMap緩衝的測試,但這個測試代碼應該還有點問題,我不知道應該怎麼改,所以發上來給大家看看,希望有點協助!
- package motor.sql;
- import java.sql.Connection;
- import java.sql.PreparedStatement;
- import java.sql.DriverManager;
- import java.sql.ResultSet;
- import java.sql.Statement;
- import java.util.*;
- import java.util.concurrent.ConcurrentHashMap;
- /**
- * @author lzk
- * ConrurrentHashMap緩衝測試程式
- *
- */
- public class BufferHashMapTest {
- private Statement stmt = null;
- private Connection con = null;
- private ResultSet rs = null;
- private String drivce = "com.microsoft.sqlserver.jdbc.SQLServerDriver";
- private String URL = "jdbc:sqlserver://127.0.0.1:1433;DatabaseName = motortestv1";
- public static ConcurrentHashMap updatehashmap = new ConcurrentHashMap();
-
- public Connection getcon(){ //得到資料庫連接
- try{
- Class.forName(drivce);
- con = DriverManager.getConnection(URL,"sa","ba123");
- }catch(Exception e){
- System.out.println(e.getMessage());
- }
- return con;
- }
-
- public ConcurrentHashMap queryHashMap(String sql) throws Exception {
- ConcurrentHashMap conhashmap = new ConcurrentHashMap();
- Connection con = new BufferHashMapTest().getcon();
- stmt = con.createStatement();
- rs = stmt.executeQuery(sql);
-
- while (rs.next()) {
- Integer array[] = new Integer[2];
- array[0] = rs.getInt(1);
- array[1] = rs.getInt(2);
- conhashmap.put(new Integer(array[0]), array[1]);
- }
- rs.close();
- return conhashmap;
- }
-
- public static void main(String []args){
- String selectsql = "select id,status from users";
- BufferHashMapTest bhmt = new BufferHashMapTest();
-
- try{
- ConcurrentHashMap conhashmap = bhmt.queryHashMap(selectsql);
-
- new Login(conhashmap).start(); //啟動登陸線程
- new Logout(conhashmap).start(); //啟動登出線程
- new updateDB(bhmt).start(); //啟動更新資料庫線程
- }catch(Exception e){
- System.out.println("Error2 :"+e.getMessage());
- }
- }
- }
- class Login extends Thread{ //登陸線程
- private ConcurrentHashMap conhashmap = null;
-
- public Login(ConcurrentHashMap conhashmap){
- this.conhashmap = conhashmap;
- }
-
- public void run(){
- try{
- while(true){
- int id = (int)(Math.random()*10000+1);
- int status = 1; //status=1表示已經登陸
- if(conhashmap.containsKey(id)){
- int values = Integer.parseInt(conhashmap.get(id).toString());
- if( values == status){
- System.out.println("使用者已經登陸!");
- }else{
- conhashmap.put(id, status); //更新記憶體中conhashmap的當前資訊
- BufferHashMapTest.updatehashmap.put(id, status); //臨時儲存更新資訊
- }
- }else{
- //System.out.println(id);
- System.out.println("id不存在!");
- }
- Thread.sleep(4);
- }
- }catch(Exception e){
- System.out.println("Error Login.run():"+e.getMessage());
- }
- }
- }
- class Logout extends Thread{ //登出線程
- private ConcurrentHashMap conhashmap = null;
-
- public Logout(ConcurrentHashMap conhashmap){
- this.conhashmap = conhashmap;
- }
-
- public void run(){
- try{
- while(true){
- int id = (int)(Math.random()*10000+1);
- int status = 0; //status=0表示已經登出
- if(conhashmap.containsKey(id)){
- int values = Integer.parseInt(conhashmap.get(id).toString());
- if(values == status){
- System.out.println("使用者已經登出!");
- }else{
- conhashmap.put(id, status); //更新記憶體中conhashmap的當前資訊
- BufferHashMapTest.updatehashmap.put(id, status); //臨時儲存更新資訊
- }
- }else{
- //System.out.println(id);
- System.out.println("id不存在!");
- }
- Thread.sleep(5);
- }
- }catch(Exception e){
- System.out.println("Error Logout.run()"+e.getMessage());
- }
- }
- }
- class updateDB extends Thread{ //更新資料庫線程
- private BufferHashMapTest bhmt = null;
- private Connection conn= null;
- private PreparedStatement pstmt = null;
- private int length = 0;
-
- public updateDB(BufferHashMapTest bhmt){
- this.bhmt = bhmt;
- }
-
- public void run(){
- try{
- while(true){
- conn = bhmt.getcon();
- length = bhmt.updatehashmap.size();
- pstmt =conn.prepareStatement("UPDATE users SET status = ? WHERE id = ?");
- Set entryset = bhmt.updatehashmap.entrySet();
- Iterator iter = entryset.iterator();
-
- while(iter.hasNext()) {
- Map.Entry entry = (Map.Entry)iter.next();
- int key = Integer.parseInt(entry.getKey().toString());
- int value = Integer.parseInt(entry.getValue().toString());
- System.out.print(key+" ");
- System.out.println(value);
- pstmt.setInt(1,value);
- pstmt.setInt(2, key);
- int tmp = pstmt.executeUpdate();
- //System.out.println(tmp);
- bhmt.updatehashmap.remove(key); //更新資料庫後移除臨時hashmap中的元素
- }
- Thread.sleep(2000);
- }
- }catch(Exception e){
- System.out.println("Error updateDB.run()"+e.getMessage());
- }
- }
- }