HashMap缓存测试

    昨天做了个HashMap缓存的测试,但这个测试代码应该还有点问题,我不知道应该怎么改,所以发上来给大家看看,希望有点帮助!
  1. package motor.sql;
  2. import java.sql.Connection;
  3. import java.sql.PreparedStatement;
  4. import java.sql.DriverManager;
  5. import java.sql.ResultSet;
  6. import java.sql.Statement;
  7. import java.util.*;
  8. import java.util.concurrent.ConcurrentHashMap;
  9. /**
  10.  * @author lzk
  11.  * ConrurrentHashMap缓存测试程序
  12.  *
  13.  */
  14. public class BufferHashMapTest {
  15.     private Statement stmt = null;
  16.     private Connection con = null;
  17.     private ResultSet rs = null;
  18.     private String drivce = "com.microsoft.sqlserver.jdbc.SQLServerDriver";
  19.     private String URL = "jdbc:sqlserver://127.0.0.1:1433;DatabaseName = motortestv1";
  20.     public static ConcurrentHashMap updatehashmap = new ConcurrentHashMap();
  21.     
  22.     public Connection getcon(){             //得到数据库连接
  23.         try{
  24.             Class.forName(drivce);
  25.             con = DriverManager.getConnection(URL,"sa","ba123");
  26.         }catch(Exception e){
  27.             System.out.println(e.getMessage());
  28.         }
  29.         return con;
  30.     }
  31.     
  32.     public ConcurrentHashMap queryHashMap(String sql) throws Exception {      
  33.         ConcurrentHashMap conhashmap = new ConcurrentHashMap();
  34.         Connection con = new BufferHashMapTest().getcon();
  35.         stmt = con.createStatement();
  36.         rs = stmt.executeQuery(sql);
  37.         
  38.         while (rs.next()) {
  39.             Integer array[] = new Integer[2];
  40.             array[0] = rs.getInt(1);
  41.             array[1] = rs.getInt(2);
  42.             conhashmap.put(new Integer(array[0]), array[1]);
  43.         }
  44.         rs.close();
  45.         return conhashmap;
  46.     }
  47.     
  48.     public static void main(String []args){
  49.         String selectsql = "select id,status from users"
  50.         BufferHashMapTest bhmt = new BufferHashMapTest();
  51.         
  52.         try{
  53.             ConcurrentHashMap conhashmap = bhmt.queryHashMap(selectsql);
  54.         
  55.             new Login(conhashmap).start();    //启动登陆线程
  56.             new Logout(conhashmap).start();   //启动注销线程
  57.             new updateDB(bhmt).start();       //启动更新数据库线程
  58.         }catch(Exception e){
  59.             System.out.println("Error2 :"+e.getMessage());
  60.         }
  61.     }
  62. }
  63. class Login extends Thread{              //登陆线程
  64.     private ConcurrentHashMap conhashmap = null;
  65.     
  66.     public Login(ConcurrentHashMap conhashmap){
  67.         this.conhashmap = conhashmap;
  68.     }
  69.     
  70.     public void run(){
  71.         try{
  72.             while(true){
  73.                 int id = (int)(Math.random()*10000+1);
  74.                 int status = 1;             //status=1表示已经登陆
  75.                 if(conhashmap.containsKey(id)){
  76.                     int values = Integer.parseInt(conhashmap.get(id).toString());
  77.                     if( values == status){
  78.                         System.out.println("用户已经登陆!");
  79.                     }else{
  80.                         conhashmap.put(id, status);                 //更新内存中conhashmap的当前信息
  81.                         BufferHashMapTest.updatehashmap.put(id, status);  //临时保存更新信息
  82.                     }
  83.                 }else{
  84.                     //System.out.println(id);
  85.                     System.out.println("id不存在!");
  86.                 }
  87.                 Thread.sleep(4);
  88.             }
  89.         }catch(Exception e){
  90.             System.out.println("Error Login.run():"+e.getMessage());
  91.         }
  92.     }
  93. }
  94. class Logout extends Thread{                 //注销线程
  95.     private ConcurrentHashMap conhashmap = null;
  96.     
  97.     public Logout(ConcurrentHashMap conhashmap){
  98.         this.conhashmap = conhashmap;
  99.     }
  100.     
  101.     public void run(){
  102.         try{
  103.             while(true){
  104.                 int id = (int)(Math.random()*10000+1);
  105.                 int status = 0;             //status=0表示已经登出
  106.                 if(conhashmap.containsKey(id)){
  107.                     int values = Integer.parseInt(conhashmap.get(id).toString()); 
  108.                     if(values == status){
  109.                         System.out.println("用户已经注销!");
  110.                     }else{
  111.                         conhashmap.put(id, status);         //更新内存中conhashmap的当前信息
  112.                         BufferHashMapTest.updatehashmap.put(id, status);  //临时保存更新信息
  113.                     }
  114.                 }else{
  115.                     //System.out.println(id);
  116.                     System.out.println("id不存在!");
  117.                 }
  118.                 Thread.sleep(5);
  119.             }
  120.         }catch(Exception e){
  121.             System.out.println("Error Logout.run()"+e.getMessage());
  122.         }
  123.     }
  124. }
  125. class updateDB extends Thread{               //更新数据库线程 
  126.     private BufferHashMapTest bhmt = null;
  127.     private Connection conn= null;    
  128.     private PreparedStatement pstmt = null;  
  129.     private int length = 0
  130.     
  131.     public updateDB(BufferHashMapTest bhmt){
  132.         this.bhmt = bhmt;
  133.     }
  134.     
  135.     public void run(){
  136.         try{
  137.             while(true){
  138.                 conn = bhmt.getcon();
  139.                 length = bhmt.updatehashmap.size();
  140.                 pstmt =conn.prepareStatement("UPDATE users SET status = ? WHERE id = ?");  
  141.                 Set entryset = bhmt.updatehashmap.entrySet(); 
  142.                 Iterator iter = entryset.iterator(); 
  143.                 
  144.                 while(iter.hasNext()) { 
  145.                     Map.Entry entry = (Map.Entry)iter.next(); 
  146.                     int key = Integer.parseInt(entry.getKey().toString());
  147.                     int value = Integer.parseInt(entry.getValue().toString());
  148.                     System.out.print(key+" ");
  149.                     System.out.println(value);
  150.                     pstmt.setInt(1,value);
  151.                     pstmt.setInt(2, key);
  152.                     int tmp = pstmt.executeUpdate();
  153.                     //System.out.println(tmp);
  154.                     bhmt.updatehashmap.remove(key);         //更新数据库后移除临时hashmap中的元素
  155.                 }
  156.                 Thread.sleep(2000);
  157.             }
  158.         }catch(Exception e){
  159.             System.out.println("Error updateDB.run()"+e.getMessage());
  160.         }
  161.     }
  162. }

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值