1

因此,我的服务器应用程序导致平均大约 95% 的巨大 cpu 使用率。我认为原因是它不断产生新线程但没有关闭它。当发生任何这种情况时,我应该如何关闭一个线程:刷新页面、注销、浏览器关闭。

对于服务器部分,我的代码或多或少是这样的。

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) {
        }
    }

这个服务器应用程序基本上会为每个线程/客户端打开一个新的并写一个日志文件。我认为问题是我在使用后没有关闭线程。这就是为什么它只是不断产生新线程..有什么帮助吗?

4

1 回答 1

1

如果我了解您要查找的内容,则可以使用超时来处理这种情况。当超时到期时,您将终止线程。

回声线

/*
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
        }
    }
于 2012-04-30T17:25:14.663 回答