Home > Software design >  How to run executorservice as non-blocking
How to run executorservice as non-blocking

Time:06-10

Please i know that similar questions has been ask here, but none of them solve my own problem, i have been trying to figure out a solution for days now and i have google a lot without solution yet. So my problem here is this, i want to send multiple http requests, get and process their results independently. But my problem is when a response from request1 for instant is being processed, request2 cannot send until the current processing response from request1 is completed.

But i make sure that i place the instance of the class processing the response inside an executor service. Then i observed that once i stop processing my response, i can send and receive multiple http request per second.

A snippet of my code

    MainPage.java
    
    class MainPage{
      MakeRequest request1 = new MakeRequest();
      MakeRequest request2 = new MakeRequest();
        
        public void processHttpResponse(String html, String sentUrl) {
         //Send result to jsoup for futher response processing
         request1.sentDataToJsoup(html, sentUrl);
    
         //i want to make another http request here while
            //the other response is been processed
            //without blocking the next http request
            //but this request is blocked from sending
            //until responce from request1 is fully processed
            request2.makeSecondRequest();
        }
    }
    
    
    MakeRequest.java
    
    class MakeRequest{

public ExecutorService executor = Executors.newFixedThreadPool(100);

        //This method sends source code to MyJsoup for processing
        public void sentDataToJsoup(final String html, final String sentUrl) {
        final Handler handler = new Handler(Looper.getMainLooper());
    
           executor.execute(new Runnable() {
                @Override
                public void run() {
    
                    handler.post(new Runnable() {
                        @Override
                        public void run() {
        new MyJsoup(html, sentUrl).processSourceCode();
                        }
                    });
                }
            });
    
            
    
            
        }
    }
    
    
    
    MyJsoup.java 
      class MyJsoup {
        
        public void processSourceCode(){
          /*
          Here i have lot of code using jsoup to extract and replace url from source code.
    
          So this is were the whole response processing is done
          */
        }
      }

So please how can i implement this so that each request will run as non-blocking script.

CodePudding user response:

Replace:

executor.execute(new Runnable() {
                @Override
                public void run() {
    
                    handler.post(new Runnable() {
                        @Override
                        public void run() {
        new MyJsoup(html, sentUrl).processSourceCode();
                        }
                    });
                }
            });

with:

executor.execute(new Runnable() {
        @Override
        public void run() {
            new MyJsoup(html, sentUrl).processSourceCode();
        }
    });
  • Related