????昨天做了个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;
-
-
-
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;?????????????
-
????????????????if(conhashmap.containsKey(id)){
-
????????????????????int?values?=?Integer.parseInt(conhashmap.get(id).toString());
-
????????????????????if(?values?==?status){
-
????????????????????????System.out.println("用户已经登陆!");
-
????????????????????}else{
-
????????????????????????conhashmap.put(id,?status);?????????????????
-
????????????????????????BufferHashMapTest.updatehashmap.put(id,?status);??
- ????????????????????}
-
????????????????}else{
-
????????????????????
-
????????????????????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;?????????????
-
????????????????if(conhashmap.containsKey(id)){
-
????????????????????int?values?=?Integer.parseInt(conhashmap.get(id).toString());?
-
????????????????????if(values?==?status){
-
????????????????????????System.out.println("用户已经注销!");
-
????????????????????}else{
-
????????????????????????conhashmap.put(id,?status);?????????
-
????????????????????????BufferHashMapTest.updatehashmap.put(id,?status);??
- ????????????????????}
-
????????????????}else{
-
????????????????????
-
????????????????????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();
-
????????????????????
-
????????????????????bhmt.updatehashmap.remove(key);?????????
- ????????????????}
-
????????????????Thread.sleep(2000);
- ????????????}
-
????????}catch(Exception?e){
-
????????????System.out.println("Error?updateDB.run()"+e.getMessage());
- ????????}
- ????}
- }
(编辑:李大同)
【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!
|