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

私があなたが探しているものを理解したなら、あなたはこの状況を処理するためにタイムアウトを使うことができます。タイムアウトが経過すると、スレッドを終了するだけです。

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
        }
    }
于 2012-04-30T17:25:14.663 に答える