Add java implementation for load balancing algorithms

This commit is contained in:
Ashish Pratap Singh
2024-06-01 23:11:42 -07:00
parent 4043fe83f0
commit 180cacd49e
5 changed files with 189 additions and 0 deletions

View File

@@ -0,0 +1,26 @@
import java.util.List;
import java.util.concurrent.atomic.AtomicInteger;
public class RoundRobin {
private List<String> servers;
private AtomicInteger index;
public RoundRobin(List<String> servers) {
this.servers = servers;
this.index = new AtomicInteger(-1);
}
public String getNextServer() {
int currentIndex = index.incrementAndGet() % servers.size();
return servers.get(currentIndex);
}
public static void main(String[] args) {
List<String> servers = List.of("Server1", "Server2", "Server3");
RoundRobin roundRobinLB = new RoundRobin(servers);
for (int i = 0; i < 6; i++) {
System.out.println(roundRobinLB.getNextServer());
}
}
}