Skip to content

Instantly share code, notes, and snippets.

@daimatz
Last active May 10, 2021 09:35
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save daimatz/9811737 to your computer and use it in GitHub Desktop.
Save daimatz/9811737 to your computer and use it in GitHub Desktop.
QPS Control example
import java.util.concurrent.Callable;
import java.util.concurrent.Executors;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.LinkedBlockingQueue;
import java.util.ArrayList;
import java.util.Timer;
import java.util.TimerTask;
import java.util.Random;
class Main {
static final int n = 20;
static final int m = 3;
static int arg = 0;
static LinkedBlockingQueue<Task> tasks = new LinkedBlockingQueue<Task>();
static ExecutorService exe = Executors.newFixedThreadPool(n);
static Random random = new Random();
public static void sendRequest(int arg) {
if (random.nextDouble() < 0.5) {
throw new RuntimeException();
}
System.out.println(arg);
}
public static void main(String[] args) {
Timer timer = new Timer();
Runner runner = new Runner();
timer.scheduleAtFixedRate(runner, 0L, 1000L);
}
static class Runner extends TimerTask {
public void run() {
System.out.println("----------------------------------------");
while (tasks.size() < n) {
tasks.offer(new Task(arg++, m));
}
ArrayList<Task> toInvoke = new ArrayList<Task>();
for (int i = 0; i < n; ++i) {
toInvoke.add(tasks.poll());
}
try {
exe.invokeAll(toInvoke);
} catch (Exception e) {
e.printStackTrace();
}
}
}
static class Task implements Callable<Void> {
private int arg;
private int nRetry;
public Task(int arg, int nRetry) {
this.arg = arg;
this.nRetry = nRetry;
}
public Void call() {
try {
sendRequest(arg);
} catch (Exception e) {
if (nRetry > 0) {
System.out.println("arg = " + arg + " failed. retrying...");
nRetry--;
tasks.offer(this);
} else {
System.out.println("arg = " + arg + " tried " + m + " times, but failed.");
}
}
return null;
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment