我的程序运行一段时间,就会僵死(就是进程还在,不处理任何客户端请求),求高人指点迷津!!! 我的服务程序如下:
public class Server { protected String SocketPort; // 监听的socket端口号
private int count = 0; //计数器 08.12.24
/**
* @param SocketPort--socket端口号
*/
public Server(String SocketPort) {
this.SocketPort = SocketPort;
} // 启动线程
public void start() {
DBCall db = null;
try {
ServerSocket Server = new ServerSocket(Integer
.parseInt(this.SocketPort));
db = new DBCall();
db.getConnection();
while (true) {
Socket s = Server.accept();
ServerThread st = new ServerThread(s, db);
st.start();
} } catch (Exception ex) {
ex.printStackTrace(); } finally {
if (db != null) {
db.closeConnection();
}
}
} // 内部线程类
class ServerThread extends Thread {
private Socket socket = null; private DBCall db = null;//数据库连接,长连接 protected InputStream streamReader; protected BufferedReader reader; ServerThread(Socket s, DBCall db) {
this.socket = s;
this.db = db;
}
/*
* (non-Javadoc)
*
* @see java.lang.Thread#run()
*/
public void run() {
Object obj = new Object();
synchronized (obj) {
while (true) {
if (Counter.parse()) {
Counter.add();
break;
} else {
try {
Thread.sleep(1000);
} catch (InterruptedException ex1) {
}
continue ;
}
}
}
int localCount = 0;
SimpleDateFormat sdf = new SimpleDateFormat("",
Locale.SIMPLIFIED_CHINESE);
sdf.applyPattern("yyyy-MM-dd HH:mm:ss");
String context = "";
File f = null;
if ("1".equals(NodeName.LOGFLAG)) {
f = new File(NodeName.LOGPATH);
}
//File f = new File("c:\\socketInfo.txt");
try {
//// 08.12.24
if (count == 10000) {
count = 0;
}
count++;
localCount = count;
FileWriter fw = null;
BufferedWriter bw = null;
if ("1".equals(NodeName.LOGFLAG)) {
if (!f.exists()) {
f.createNewFile();
if (!f.exists()) {
System.out.println("建立socketInfo.txt文件失败!");
}
}
fw = new FileWriter(f, true);
bw = new BufferedWriter(fw); Date d = new java.sql.Date(System.currentTimeMillis());
String timeStr = sdf.format(d);
System.out.println("NO" + localCount +
":go in ServerScocke=" +
timeStr); bw.write("\r\n");
context = "NO" + localCount + ":go in ServerScocke=" +
timeStr;
bw.write(context);
bw.flush(); }
//// 08.12.24
streamReader = socket.getInputStream(); reader = new BufferedReader(new InputStreamReader(streamReader,
"UTF-8")); String msg = "";
StringBuffer msgBuffer = new StringBuffer();
while ((msg = reader.readLine()) != null) {
msgBuffer.append(msg);
} String phone = ParseXMLStr.parse(msgBuffer.toString(),
NodeName.node1);
String fmt = ParseXMLStr.parse(msgBuffer.toString(),
NodeName.node2);
String data = ParseXMLStr.parse(msgBuffer.toString(),
NodeName.node3); boolean result = false;
try {
if (db.con == null) {
db.getConnection();
}
result = db.callprodure(phone, fmt, data, localCount);
if (result) {
System.out.println("sucessfully" + " phone:" + phone
+ " fmt: " + fmt + " data: " + data);
} else {
System.out.println("error" + " phone:" + phone
+ " fmt: " + fmt + " data: " + data);
}
} catch (SQLException e) {
e.printStackTrace();
} } catch (IOException e) {
e.printStackTrace();
} finally {
try {
reader.close();
} catch (IOException e) {
e.printStackTrace();
}
try {
streamReader.close();
} catch (IOException e) {
e.printStackTrace();
}
try {
socket.close();
} catch (IOException e) {
e.printStackTrace();
}
}
Counter.sub();
}
} 1其中重要的Counter类如下: public class Counter {
public static int counter=0;
public static int max=new Integer(NodeName.THREADNUM).intValue();
public synchronized static boolean parse(){
if(Counter.counter < Counter.max){
return true;
}else{
return false;
}
}
public synchronized static void add(){
Counter.counter += 1;
}
public synchronized static void sub(){
Counter.counter-=1;
}
} 是不是Counter类中的conuter变量并发引起的啊???
高人指点啊,事情紧急,
public class Server { protected String SocketPort; // 监听的socket端口号
private int count = 0; //计数器 08.12.24
/**
* @param SocketPort--socket端口号
*/
public Server(String SocketPort) {
this.SocketPort = SocketPort;
} // 启动线程
public void start() {
DBCall db = null;
try {
ServerSocket Server = new ServerSocket(Integer
.parseInt(this.SocketPort));
db = new DBCall();
db.getConnection();
while (true) {
Socket s = Server.accept();
ServerThread st = new ServerThread(s, db);
st.start();
} } catch (Exception ex) {
ex.printStackTrace(); } finally {
if (db != null) {
db.closeConnection();
}
}
} // 内部线程类
class ServerThread extends Thread {
private Socket socket = null; private DBCall db = null;//数据库连接,长连接 protected InputStream streamReader; protected BufferedReader reader; ServerThread(Socket s, DBCall db) {
this.socket = s;
this.db = db;
}
/*
* (non-Javadoc)
*
* @see java.lang.Thread#run()
*/
public void run() {
Object obj = new Object();
synchronized (obj) {
while (true) {
if (Counter.parse()) {
Counter.add();
break;
} else {
try {
Thread.sleep(1000);
} catch (InterruptedException ex1) {
}
continue ;
}
}
}
int localCount = 0;
SimpleDateFormat sdf = new SimpleDateFormat("",
Locale.SIMPLIFIED_CHINESE);
sdf.applyPattern("yyyy-MM-dd HH:mm:ss");
String context = "";
File f = null;
if ("1".equals(NodeName.LOGFLAG)) {
f = new File(NodeName.LOGPATH);
}
//File f = new File("c:\\socketInfo.txt");
try {
//// 08.12.24
if (count == 10000) {
count = 0;
}
count++;
localCount = count;
FileWriter fw = null;
BufferedWriter bw = null;
if ("1".equals(NodeName.LOGFLAG)) {
if (!f.exists()) {
f.createNewFile();
if (!f.exists()) {
System.out.println("建立socketInfo.txt文件失败!");
}
}
fw = new FileWriter(f, true);
bw = new BufferedWriter(fw); Date d = new java.sql.Date(System.currentTimeMillis());
String timeStr = sdf.format(d);
System.out.println("NO" + localCount +
":go in ServerScocke=" +
timeStr); bw.write("\r\n");
context = "NO" + localCount + ":go in ServerScocke=" +
timeStr;
bw.write(context);
bw.flush(); }
//// 08.12.24
streamReader = socket.getInputStream(); reader = new BufferedReader(new InputStreamReader(streamReader,
"UTF-8")); String msg = "";
StringBuffer msgBuffer = new StringBuffer();
while ((msg = reader.readLine()) != null) {
msgBuffer.append(msg);
} String phone = ParseXMLStr.parse(msgBuffer.toString(),
NodeName.node1);
String fmt = ParseXMLStr.parse(msgBuffer.toString(),
NodeName.node2);
String data = ParseXMLStr.parse(msgBuffer.toString(),
NodeName.node3); boolean result = false;
try {
if (db.con == null) {
db.getConnection();
}
result = db.callprodure(phone, fmt, data, localCount);
if (result) {
System.out.println("sucessfully" + " phone:" + phone
+ " fmt: " + fmt + " data: " + data);
} else {
System.out.println("error" + " phone:" + phone
+ " fmt: " + fmt + " data: " + data);
}
} catch (SQLException e) {
e.printStackTrace();
} } catch (IOException e) {
e.printStackTrace();
} finally {
try {
reader.close();
} catch (IOException e) {
e.printStackTrace();
}
try {
streamReader.close();
} catch (IOException e) {
e.printStackTrace();
}
try {
socket.close();
} catch (IOException e) {
e.printStackTrace();
}
}
Counter.sub();
}
} 1其中重要的Counter类如下: public class Counter {
public static int counter=0;
public static int max=new Integer(NodeName.THREADNUM).intValue();
public synchronized static boolean parse(){
if(Counter.counter < Counter.max){
return true;
}else{
return false;
}
}
public synchronized static void add(){
Counter.counter += 1;
}
public synchronized static void sub(){
Counter.counter-=1;
}
} 是不是Counter类中的conuter变量并发引起的啊???
高人指点啊,事情紧急,
是不是数据库中出现了锁,你查看一下,还有你的server用在什么操作系统上,你的jdk是什么版本的?
似乎从没见过bw.close();
这样会有问题的,把所有的文件句柄都占用了,也无法提供服务。
如果断连接就可以重连。本人推荐使用短连接。