package Thread;import java.io.IOException;import Main.Main;public class MyThread extends Thread{
public void run() {
for (;;) {
Main m = new Main();
if(m.connA()){
System.out.println("成功");
}else{
System.out.println("失败");
}
try {
sleep(5 * 1000);
} catch (Exception e) {
System.out.println(e);
}
}
}
public static void main(String[] args) {
char exit = 'e';
Thread t = new MyThread();
t.setDaemon(true);
//t.run();
t.start();
while (exit != 'Q' && exit != 'q') {
try {
System.out.println("Press   Q/q   to   exit:");
exit = (char) System.in.read();
} catch (IOException e) {
System.out.println(e);
}
}
System.out.println("Process   finished.");
}
}
package Main;import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.HashMap;import ThreadLocal.MyThreadLocal;import DBTools.DBTools;public class Main {
// @SuppressWarnings("static-access")
HashMap<String, DBTools> map = new HashMap<String, DBTools>();
DBTools db = new DBTools();
public Boolean connA() {
map.put("A", db);
MyThreadLocal.setCurrentConnection(map);
try {
Statement state = ((DBTools) MyThreadLocal.getCurrentConnection().get("A")).getStatementB();
String sql = "select * from CUSTMOER_DT ";
ResultSet rs = state.executeQuery(sql);
if (rs.next()) {
System.out.println(rs.getString(2));
} else {
System.out.println("error");
return (false);
} } catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
return (false);
}
System.out.println("ccccc");
return (true);
} public Boolean connB() {
return true;
}}
package ThreadLocal;
import java.util.HashMap;
/** * 用于存放线程内的信息 * @author keyboardsun * */ 
public class MyThreadLocal { /** * 存放当前线程的连接 */ 
private static final ThreadLocal currentConnection = new ThreadLocal(); /** * 存放当前线程的用户信息 */ 
/** * 获取当前的连接 * @return 获取当前线程的数据库连接 */ 
public static HashMap getCurrentConnection(){ 
return (HashMap)currentConnection.get(); 

/** 设置当前的连接 * @param map */ 
public static void setCurrentConnection(HashMap map){ 
currentConnection.set(map);
}
}
在main方法中 如果调用t.run();程序正常转,但是调用t.start();的时候程序就无法正常转, 用debug跟踪发现一到Statement state = ((DBTools) MyThreadLocal.getCurrentConnection().get("A")).getStatementB();这个地方就停大约一秒再往下走,大家帮忙看看这是怎么回事