日期:2014-05-20  浏览次数:20740 次

请教大家关于HashMap做缓存的测试,看看我这程序有什么问题
昨天做了个HashMap缓存的测试,但这个测试代码应该还有点问题,我不知道应该怎么改,所以发上来给大家看看,希望有点帮助!
<code>
package task0919;
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()*1000+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(3);
}
}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()*1000+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); //临时保存更新信息