如何限制线程池中一秒钟只运行十个线程,参考代码如下:
package cn.starty.test;
import java.util.Date;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.Semaphore;
import java.util.concurrent.TimeUnit;
public class TestOne {
final static int MAX_QPS = 10;
final static Semaphore semaphore = new Semaphore(MAX_QPS);
public static void main(String[] args) throws InterruptedException {
Executors.newScheduledThreadPool(1).scheduleAtFixedRate(new Runnable() {
@Override
public void run() {
semaphore.release(MAX_QPS / 2);
}
}, 1000, 500, TimeUnit.MILLISECONDS);
ExecutorService pool = Executors.newFixedThreadPool(100);
for (int i = 100; i > 0; i--) {
final int x = i;
pool.submit(new Runnable() {
@Override
public void run() {
for (int j = 1000; j > 0; j--) {
semaphore.acquireUninterruptibly(1);
remoteCall(x, j);
}
}
});
}
pool.shutdown();
pool.awaitTermination(1, TimeUnit.HOURS);
System.out.println("DONE");
}
private static void remoteCall(int i, int j) {
System.out.println(String.format("%s - %s: %d %d", new Date(),
Thread.currentThread(), i, j));
}
}