-1

I do have access to few Servers say A, B, C, D and E. I'd like to choose data from these servers one by one in a round robin way. I am new to Java and threads, It would be a great help if you could help me with this.

What I am trying to do is to load a map in my application, I send HTTP requests to the servers. These Servers revert response in Bitmap format, I arrange these Images (Tiles) and show it in my application, but I am doing it it sequentially. E.g. I request Server A first to get the tiles then Server B and so on..I would like to get the tiles in such a way that Server A downloads one Image, Server B does the other. If I if I'd be doing it all alone using one server without using Multithreading it would take a long time to display whole Map.

Yauraw Gadav
  • 1,706
  • 1
  • 18
  • 39

2 Answers2

2

Create a url builder which has the base urls of each server in an array and also keeps track of which server was hit last time. Next time you need data, just return the base url of the next server.

Mus
  • 1,860
  • 2
  • 16
  • 19
1

use modulo see example: (used String as the url)

public static final int MAX_SERVER = 4;

public static void main(String[] args)
{

    String urlarr[] = new String[MAX_SERVER];
    init(urlarr);

    int idx = 0;
    while(idx < 1000){
        String next = urlarr[idx++%urlarr.length];
        System.out.println(next);
    }
}

private static void init(String[] urlarr)
{
    for(int i=0 ; i<urlarr.length ; i++){
        urlarr[i] = "url("+i+")";
    }

}

using module size of array on idx make it iterates over all available indexes 0,1,2,3 in this case. part of output:

url(0)
url(1)
url(2)
url(3)
url(0)
url(1)
url(2)
url(3)
roni bar yanai
  • 1,492
  • 10
  • 12