线程睡眠使主服务器进入睡眠状态

时间:2015-12-05 18:30:34

标签: java multithreading server thread-sleep java-server

我想编写一个java多线程服务器。我用了这段代码:

MultiThreadedServer.java:

package servers;

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

public class MultiThreadedServer implements Runnable{

    protected int          serverPort   = 8080;
    protected ServerSocket serverSocket = null;
    protected boolean      isStopped    = false;
    protected Thread       runningThread= null;

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

    public void run(){
        synchronized(this){
            this.runningThread = Thread.currentThread();
        }
        openServerSocket();
        while(! isStopped()){
            Socket clientSocket = null;
            try {
                clientSocket = this.serverSocket.accept();
            } catch (IOException e) {
                if(isStopped()) {
                    System.out.println("Server Stopped.") ;
                    return;
                }
                throw new RuntimeException(
                    "Error accepting client connection", e);
            }
            System.out.println("Request Entered: " + System.currentTimeMillis());
            new Thread(
                new WorkerRunnable(
                    clientSocket, "Multithreaded Server")
            ).start();
        }
        System.out.println("Server Stopped.") ;
    }


    private synchronized boolean isStopped() {
        return this.isStopped;
    }

    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) {
            throw new RuntimeException("Cannot open port 8080", e);
        }
    }

}

WorkerRunnable.java:

package servers;

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

public class WorkerRunnable implements Runnable{
protected Socket clientSocket = null;
protected String serverText   = null;

public WorkerRunnable(Socket clientSocket, String serverText) {
    this.clientSocket = clientSocket;
    this.serverText   = serverText;
}

public void run() {
    try {
        InputStream input  = clientSocket.getInputStream();
        OutputStream output = clientSocket.getOutputStream();
        long time = System.currentTimeMillis();
        output.write(("HTTP/1.1 200 OK\n\nWorkerRunnable: " + this.serverText + " - " +time +"").getBytes());
        Thread.sleep(2000);
        output.close();
        input.close();
        System.out.println("Request processed: " + time);
    } catch (IOException | InterruptedException e) {
        //report exception somewhere.
        e.printStackTrace();
    }
}
}

我使用此代码运行主服务器:

    MultiThreadedServer server = new MultiThreadedServer(9000);
    new Thread(server).start();

然后我在网络浏览器中打开了3个标签,并在所有标签中输入http://localhost:9000/。 但制表符不会同时运行,并且会在2秒后收到响应。

我发现当我在每个WorkerRunnable线程中运行“Thread.sleep”时,MultiThreadedServer不再接受任何请求。 任何同时运行所有线程的解决方案?

0 个答案:

没有答案