因此,我的服务器应用程序导致巨大的CPU使用率平均约为95%。我认为原因是它继续产生新线程但不关闭它。当发生任何这种情况时,我怎么想关闭一个线程:刷新页面,注销,浏览器关闭。
对于服务器部分,我的代码或多或少都是这样。
ThreadedEchoServer.java
public class ThreadedEchoServer {
// using port 2000
static final int PORT = 2000;
public static void main(String args[]) {
ServerSocket serverSocket = null;
Socket socket = null;
try {
serverSocket = new ServerSocket(PORT);
} catch (IOException e) {
e.printStackTrace();
}
while (true) {
try {
socket = serverSocket.accept();
} catch (IOException e) {
System.out.println("I/O error: " + e);
}
// new thread for a client
new EchoThread(socket).start();
}
}
}
EchoThread.java
/*
Class for java server to accept incoming stream
and create a new thread to save log file data in server
*/
public class EchoThread extends Thread {
protected Socket socket;
public EchoThread(Socket clientSocket) {
this.socket = clientSocket;
}
public void run() {
/*Create a File*/
int i = 1;
try {
File directory = new File("F:/temp/tmplog/" + getDate());
if(!directory.exists()) {
directory.mkdir();
}
String fileName = "F:/temp/tmplog/" + getDate() + "/" + getDateTime() + ".txt";
File file = new File(fileName);
//Double Make Sure it create the file
while(file.exists()) {
file = new File(fileName + "." + i);
i++;
}
FileOutputStream fis = new FileOutputStream(file, true);
PrintStream out = new PrintStream(fis);
System.setOut(out);
while (true) {
try {
InputStream is = socket.getInputStream();
BufferedReader br = new BufferedReader(new InputStreamReader(is, "US-ASCII"));
String line = null;
while ((line = br.readLine()) != null) {
System.out.println(line);
}
} catch (IOException exception) {
// Just handle next request.
} finally {
if (socket != null) {
try {
socket.close();
} catch (IOException ignored) {
}
}
fis.close();
}
}
} catch (IOException ignored) {
}
}
此服务器应用程序基本上将打开一个新的并为每个线程/客户端写一个日志文件。我认为问题是我在使用后没有关闭线程。这就是为什么它只是继续产生新线程..任何帮助?
答案 0 :(得分:1)
如果我理解你在寻找什么,你可以使用超时来处理这种情况。 当超时到期时,您将终止该线程。
EchoThread
/*
Class for java server to accept incoming stream
and create a new thread to save log file data in server
*/
public class EchoThread extends Thread {
protected Socket socket;
public EchoThread(Socket clientSocket) {
this.socket = clientSocket;
this.socket.setSoTimeout(10000); //Sets timeout to 10 seconds
}
public void run() {
/*Create a File*/
int i = 1;
try {
File directory = new File("F:/temp/tmplog/" + getDate());
if(!directory.exists()) {
directory.mkdir();
}
String fileName = "F:/temp/tmplog/" + getDate() + "/" + getDateTime() + ".txt";
File file = new File(fileName);
//Double Make Sure it create the file
while(file.exists()) {
file = new File(fileName + "." + i);
i++;
}
FileOutputStream fis = new FileOutputStream(file, true);
PrintStream out = new PrintStream(fis);
System.setOut(out);
while (true) {
try {
InputStream is = socket.getInputStream();
BufferedReader br = new BufferedReader(new InputStreamReader(is, "US-ASCII"));
String line = null;
while ((line = br.readLine()) != null) {
System.out.println(line);
}
} catch (IOException exception) {
// Just handle next request.
} finally {
if (socket != null) {
try {
socket.close();
} catch (IOException ignored) {
}
}
fis.close();
}
}
} catch (IOException ignored) {
} catch (SocketException e) { //Exception thrown by timeout
socket.close(); //We close the socket
this.stop(); //We stop the thread
}
}