0

I have this code below which makes 300 http requests and each request returns 10000 rows from database. Total size of 10000 is approximately 0.4mb. So 300*0.4 = 120mb.

Questions:

  1. How increasing the ThreadPool size for handing requests in Volley, can affect the perfomance in app? I change it to 12, but the execution time and size of data was the same as with 4. Is there any difference at all?
  2. When in creasing the number of Volley threads, does the resulted data increase as well? If had 1 thread the maximum returned data each time would be 0.4mb. But if we had 4, the maximum would be 1.6mb.

Emulator: 4 Cores MultiThread

ExecutorService service  = Executors.newFixedThreadPool(4);
RequestQueue queue;
AtomicInteger counter = new AtomicInteger(0);

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);


    File cacheDir = new File(this.getCacheDir(), "Volley");
    queue = new RequestQueue(new DiskBasedCache(cacheDir), new BasicNetwork(new HurlStack()), 4);
    queue.start();
    start();
}



public void start(){

    String url ="...";
    for(int i =0 ; i<300; i++) {
        counter.incrementAndGet();
        StringRequest stringRequest = new StringRequest(Request.Method.GET, url,
                new Response.Listener<String>() {
                    @Override
                    public void onResponse(String response) {

                        method(response);
                    }
                }, new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError error) {
                Log.d("VolleyError", error.toString());
            }
        });
        stringRequest.setTag("a");
        queue.add(stringRequest);
    }
}

public synchronized void decreased(){

    if(counter.decrementAndGet()==0)
        start();
}

public void method( String response){

        Runnable task = new Runnable() {
            @Override
            public void run() {

                List<Customer> customers= new ArrayList<>();
                ObjectMapper objectMapper = new ObjectMapper();
                TypeFactory typeFactory = objectMapper.getTypeFactory();
                try {
                customers= objectMapper.readValue(response, new TypeReference<List<Customer>>() {});

                              //Simulate database insertion delay
                              try {
                                    Thread.sleep(1000);
                                } catch (InterruptedException e) {
                                   e.printStackTrace();
                                }

                    decreased();
                } catch (IOException e1) {
                    e1.printStackTrace();
                }
            }
        };


    logHeap("");
    service.execute(task);

}
Nick
  • 2,818
  • 5
  • 42
  • 60

1 Answers1

0

Regarding Question 1: Thread pool with size 4 will be better when compared to 12. Thread pool size should be in conjunction with number of processors available.

Since the number of processors are limited, app should not spawn unnecessary threads as this may lead to performance problem. As android OS has to manage resource between more threads which will lead to increased wait and actual time for each thread.

Ideally assuming your threads do not have locking such that they do not block each other (independent of each other) and you can assume that the work load (processing) is same, then it turns out that, have a pool size of Runtime.getRuntime().availableProcessors() or availableProcessors() + 1 gives the best results. Please refer link Setting Ideal size of Thread Pool for more info.

Regarding question 2: If I have understood your question correctly, there should be no change on returned data as thread pool size has no effect on network payload, only wait time and actual time will be changed, when thread pool size value is changed.

abhishesh
  • 3,246
  • 18
  • 20
  • Lets not "assume" anything and focus on the main question here. Im talking about specifically Volley. I dont know if the threads are blocking each other. – Nick Jul 09 '18 at 12:18
  • In case of volley, thread pool size should be close to available processors, also there would be unnecessary overhead of creating more threads. – abhishesh Jul 09 '18 at 12:24
  • "thread pool size has no effect on network payload". But thread pool parses the responses from the server and then send them to the main thread. So if we had 12 threads, 12 concurrent responses will be parse and send to main Thread. So if main Thread is not fast enough to execute responses ,memory will keep rising. Am I right? – Nick Jul 09 '18 at 12:26
  • All 12 threads would not be in RUNNING state at same time, as threads run on processors, at most Runtime.getRuntime().availableProcessors() will be in running state, other would be in waiting state. – abhishesh Jul 09 '18 at 12:29
  • okk. One last question. Does network latency affect the performance also?. If responses are slow, 4 Threads would be as good as 1. Right? – Nick Jul 09 '18 at 12:36
  • Network latency surely affects performance and user experience as well. Since 4 threads are running parallel, they would be always better than 1 if you have 4 network request jobs. – abhishesh Jul 09 '18 at 12:51
  • Let us [continue this discussion in chat](https://chat.stackoverflow.com/rooms/174655/discussion-between-abhishesh-and-nick). – abhishesh Jul 09 '18 at 12:55