3

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 ..

4
  • 1
    First, why not use an ExecutorService rather than your own weird code? I would suspect that your work isn't quite long-running enough for things to happen concurrently. Commented Mar 24, 2014 at 22:39
  • You shouldn't extend Thread, just implement Runnable and supply that to the constructor of the normal Thread class. Commented Mar 25, 2014 at 9:08
  • Where is the bold line? And how are you making request to Java? Commented Mar 25, 2014 at 9:38
  • i send that via PHP socket method as like as write msg .. Commented Mar 25, 2014 at 15:20

2 Answers 2

1

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.

Sign up to request clarification or add additional context in comments.

3 Comments

the reqouest was sent by PHP in socket , I'm new in Java and the PHP is a web server and it's normal to send some process in same time ,
@kiamoz Glad the idea helped. I don't think using a queue is a bad idea. Make sure your queuing rule do not cause "collisions". Implementing a queue to serve requests can get tricky.
I'm new in java but Im going to lear those Trick :D , thank thank thank
1

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.