multithreading - java multi threading issue ( same run time ) -


i have socket server in java , other side socket client in php

i want process socket request php in java in same time multi-threading java 1 one , wait finish first request , start second 1 ,

here code in java :

while (true) {     try {         clientsocket = serversocket.accept();         int = 0;         (i = 0; < 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 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 example of output , want start output of multi started request in same time in same time .. java wait finish request in 1 time code ..

i don't see why you'd want more 2 threads here.

if want process request 1 one, might spawn 1 thread listens requests , responds sending "processing" or "check later" message. (call listener thread)

if client sent "processing" response connection kept alive , thread spawned responds client actual processed result of request. (call processing thread).

you make listener thread send keep alive message client in queue or ask check after set period of time request. make listener thread more sophisticated setting queues decide when subsequently respond clients sent "check later" message

from implementation pov, main thread listener thread , spawn processing thread when it's time process request.


Comments

Popular posts from this blog

android - Get AccessToken using signpost OAuth without opening a browser (Two legged Oauth) -

org.mockito.exceptions.misusing.InvalidUseOfMatchersException: mockito -

google shop client API returns 400 bad request error while adding an item -