kiamoz
kiamoz

Reputation: 714

java multi threading issue ( same run time )

I have socket server in Java and other side socket client in PHP

I want to process socket request from PHP in java in same time by multi-threading but java do it one by one , wait to finish first request and the start second one ,

here is my code in JAVA :

while (true) {
    try {
        clientSocket = serverSocket.accept();
        int i = 0;
        for (i = 0; i < maxClientsCount; i++) {
            if (threads[i] == null) {
                (threads[i] = new clientThread(clientSocket, threads)).start();
                break;
            }
        }
        if (i == maxClientsCount) {
            PrintStream os = new PrintStream(clientSocket.getOutputStream());
            os.println("Server too busy. Try later.");
            os.close();
            clientSocket.close();
        }
    } catch (IOException e) {
        System.out.println(e);
    }
}


class clientThread extends Thread {
    public clientThread(Socket clientSocket, clientThread[] threads) {
        this.clientSocket = clientSocket;
        this.threads = threads;
        maxClientsCount = threads.length;
    }

    public void run() {
        int maxClientsCount = this.maxClientsCount;
        clientThread[] threads = this.threads;
        try {
            in = new BufferedReader(new InputStreamReader(this.clientSocket.getInputStream()));
            URL aURL = new URL(RecivedURL);
            // start out put
            System.out.println("host = " + aURL.getHost());
            // end out put

the BOLD line is example of my output , but I want to start output of multi started request in same time in same time .. JAvA wait to finish a request in one time for my code ..

Upvotes: 3

Views: 134

Answers (2)

Christian
Christian

Reputation: 3721

I assume that it's executed so fast that the last request is finished before the next one can be accepted.

For debug purposes try to add:

try {
  Thread.sleep(1000);
} catch (InterruptedException e) {
  Thread.currentThread().interrupt();
}

into the run method so you can easier check if it's really not running in parallel.

Upvotes: 1

anu
anu

Reputation: 1007

I don't see why you'd want more than two threads here.

If you want to process request one by one, you might spawn just one thread that just listens the requests and immediately responds to it by sending a "processing" or a "check back later" message. (call this a listener thread)

if a client is sent a "processing" response the connection is kept alive and another thread is spawned that responds to the client with the actual processed result of request. (call this a processing thread).

You could make the listener thread send a keep alive message to the client in queue or you could ask it to check back after a set period of time with a request. You could make the listener thread more sophisticated by setting up queues to decide when to subsequently respond to clients who were sent "check back later" message

From implementation POV, your main thread could be your listener thread and it could spawn a processing thread when it's time to process a request.

Upvotes: 1

Related Questions