Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/multithreading/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java多线程服务器未按预期工作_Java_Multithreading - Fatal编程技术网

Java多线程服务器未按预期工作

Java多线程服务器未按预期工作,java,multithreading,Java,Multithreading,我正在尝试编写一个多线程服务器,它应该能够一次接受多个HTTP请求 服务器代码: package test.thread.server; import java.io.IOException; import java.io.InputStream; import java.io.OutputStream; import java.net.ServerSocket; import java.net.Socket; public class MyS

我正在尝试编写一个多线程服务器,它应该能够一次接受多个HTTP请求

服务器代码:

package test.thread.server;

    import java.io.IOException;
    import java.io.InputStream;
    import java.io.OutputStream;
    import java.net.ServerSocket;
    import java.net.Socket;

    public class MyServer implements Runnable {
        private int serverPort = 8080;
        private ServerSocket serverSocket;
        private Thread runningThread;
        private boolean isStopped;

        public MyServer(int port){
            this.serverPort = port;
        }


        @Override
        public void run() {
            synchronized (this) {
                this.runningThread = Thread.currentThread();
            }

            openServerSocket();


            while(!isStopped){

                Socket clientSocket = null;

                try {
                    clientSocket = this.serverSocket.accept();
                } catch (IOException e) {
                    e.printStackTrace();
                }


                //start a new thread for processing each request
                new Thread(new RequestHandler(clientSocket)).start();
            }

        }

    public synchronized void stop(){
        this.isStopped = true;
        try {
            this.serverSocket.close();
        } catch (IOException e) {
            throw new RuntimeException("Error closing server", e);
        }
    }


    private void openServerSocket(){
        try {
            this.serverSocket = new ServerSocket(this.serverPort);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

}
Server: Wed Feb 15 18:17:06 IST 2017: Thread-1 - Started : Input
Client: Wed Feb 15 18:17:36 IST 2017: Output
Server:Wed Feb 15 18:17:36 IST 2017: Thread-2 - Started : Input
Client:Wed Feb 15 18:18:06 IST 2017: Output
Server:Wed Feb 15 18:18:06 IST 2017: Thread-3 - Started : Input
Client:Wed Feb 15 18:18:36 IST 2017: Output
Server:Wed Feb 15 18:18:43 IST 2017: Thread-4 - Started : Input
Client:Wed Feb 15 18:19:13 IST 2017: Output
Server:Wed Feb 15 18:19:13 IST 2017: Thread-5 - Started : Input
Client:Wed Feb 15 18:19:43 IST 2017: Output
处理请求的工作者:

package test.thread.server;

    import java.io.IOException;
    import java.io.InputStream;
    import java.io.OutputStream;
    import java.net.ServerSocket;
    import java.net.Socket;

    public class MyServer implements Runnable {
        private int serverPort = 8080;
        private ServerSocket serverSocket;
        private Thread runningThread;
        private boolean isStopped;

        public MyServer(int port){
            this.serverPort = port;
        }


        @Override
        public void run() {
            synchronized (this) {
                this.runningThread = Thread.currentThread();
            }

            openServerSocket();


            while(!isStopped){

                Socket clientSocket = null;

                try {
                    clientSocket = this.serverSocket.accept();
                } catch (IOException e) {
                    e.printStackTrace();
                }


                //start a new thread for processing each request
                new Thread(new RequestHandler(clientSocket)).start();
            }

        }

    public synchronized void stop(){
        this.isStopped = true;
        try {
            this.serverSocket.close();
        } catch (IOException e) {
            throw new RuntimeException("Error closing server", e);
        }
    }


    private void openServerSocket(){
        try {
            this.serverSocket = new ServerSocket(this.serverPort);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

}
Server: Wed Feb 15 18:17:06 IST 2017: Thread-1 - Started : Input
Client: Wed Feb 15 18:17:36 IST 2017: Output
Server:Wed Feb 15 18:17:36 IST 2017: Thread-2 - Started : Input
Client:Wed Feb 15 18:18:06 IST 2017: Output
Server:Wed Feb 15 18:18:06 IST 2017: Thread-3 - Started : Input
Client:Wed Feb 15 18:18:36 IST 2017: Output
Server:Wed Feb 15 18:18:43 IST 2017: Thread-4 - Started : Input
Client:Wed Feb 15 18:19:13 IST 2017: Output
Server:Wed Feb 15 18:19:13 IST 2017: Thread-5 - Started : Input
Client:Wed Feb 15 18:19:43 IST 2017: Output
它从输入流中读取数据并打印。之后,它应该进行30秒的睡眠[表示一些不需要CPU的工作]。睡眠时间过后,服务器将响应客户端

package test.thread.server;

import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.Socket;
import java.util.Date;

public class RequestHandler implements Runnable {
    Socket clientSocket;

    static int counter = 0;

    public RequestHandler(Socket clientSocket){
        this.clientSocket = clientSocket;
    }

    @Override
    public void run() {

        try {
            InputStream input = this.clientSocket.getInputStream();
            OutputStream output = this.clientSocket.getOutputStream();


            DataInputStream inFromClient = new DataInputStream(input);
            System.out.println(new Date()+": " +Thread.currentThread().getName() + " - Started : "+inFromClient.readUTF());

            Thread.sleep(30000);

            /*output.write(("HTTP/1.1 200 OK\n\n<html><body>" +
                    "Multi-threaded Server " +
                    "</body></html>").getBytes());*/

            DataOutputStream outFromServer = new DataOutputStream(output);
            outFromServer.writeUTF("Output");
            outFromServer.flush();

            output.close();
            input.close();

        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (InterruptedException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }


    }

}

有什么问题吗?

我相信问题出在你的客户身上。客户端仅在前一个请求得到响应后发送下一个请求。
informserver.readUTF()
会一直阻塞,直到数据可用为止,只有当服务器发送实际答案时才会出现这种情况


我建议您调试程序。

我相信问题出在您的客户端。客户端仅在前一个请求得到响应后发送下一个请求。
informserver.readUTF()
会一直阻塞,直到数据可用为止,只有当服务器发送实际答案时才会出现这种情况


我建议您调试您的程序。

Haaaaa。。。。客户是罪魁祸首。我怀疑我的服务器,花了2个多小时调试它。我应该在不同的线程上启动客户端请求!!!。。。。我的错,哈哈。。。。客户是罪魁祸首。我怀疑我的服务器,花了2个多小时调试它。我应该在不同的线程上启动客户端请求!!!。。。。我的错。