所以,我的服务器应用程序造成了巨大的 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) {
}
}
该服务器应用程序基本上将为每个线程/客户端打开一个新文件并写入一个日志文件。我认为问题是我在使用后没有关闭线程。这就是为什么它不断产生新线程......有什么帮助吗?
最佳答案
如果我明白你在寻找什么,你可以使用超时来处理这种情况。 当超时到期时,您将终止线程。
回声线程
/*
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
}
}
关于java - java多线程环境下如何关闭线程,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10387647/