我的程序运行一段时间,就会僵死(就是进程还在,不处理任何客户端请求),求高人指点迷津!!!我的服务程序如下:
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变量并发引起的啊???
高人指点啊,事情紧急,
import java.net.*;
import java.io.*;
public class oneServerThread extends Thread{//发送数据
private Socket s;
private QQServerFrame QQ;
public oneServerThread(Socket s,QQServerFrame QQ) {
this.s=s;
this.QQ=QQ;
}
public void run(){
try{
String info=QQ.txtUserSend.getText();
BufferedReader in=new BufferedReader(new InputStreamReader(System.in,info));
PrintWriter out=new PrintWriter(s.getOutputStream());
while(true){
String str=in.readLine();
out.println(str);
QQ.txtInfoShow.append(str+"\n");
if(str.equals("bye")){
break;
}
}
in.close();
out.close();
}catch(Exception se){
se.printStackTrace();
}
}
}
----------------------------
package chatqq;import java.net.*;
import java.io.*;
public class ClientThread extends Thread{//接Server 数据
private PrintWriter out = null;
private Socket s = null;
private BufferedReader in;
private String str=null;
private QQClientFrame QQ=null;
public ClientThread(Socket s,QQClientFrame QQ){
this.s = s;
this.QQ=QQ;
}
public void run(){
try {
in = new BufferedReader(new InputStreamReader(s.getInputStream()));
out = new PrintWriter(s.getOutputStream(),true);
out.println(str);
while(true){
str=in.readLine();
if(str.equals("bye")){
str="client from Server1"+str;
out.println(str);
QQ.txtInfoShow.append(str+"\n");
break;
}else{
str="client from Server2"+str;
out.println(str);
QQ.txtInfoShow.append(str+"\n");
}
this.stop();
}
in.close();
out.close();
s.close();
} catch (IOException ioe) {
ioe.printStackTrace();
} }}看看这个就OK了..