Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package com.j256;
- import java.util.ArrayList;
- import java.util.Iterator;
- import java.util.List;
- import java.util.concurrent.Callable;
- import java.util.concurrent.ExecutorService;
- import java.util.concurrent.Executors;
- import java.util.concurrent.Future;
- /**
- * Class to show how you can process a large number of requests while keeping the memory
- * requirements down. Right now it sleeps and loops but you could use a notify/wait to
- * speed up the loop a bit.
- */
- public class LimitedOutandingTasks {
- private static final int NUM_THREADS = 200;
- private static final int NUM_JOBS = 100000;
- private static final int MAX_OUTSTANDING = 1000;
- private static final long SLEEP_BETWEEN_REAPS = 10;
- public static void main(String[] args) throws Exception {
- new LimitedOutandingTasks().doMain(args);
- }
- private void doMain(String[] args) throws Exception {
- ExecutorService pool = Executors.newFixedThreadPool(NUM_THREADS);
- List<Future<Result>> futureList = new ArrayList<Future<Result>>();
- int jobC = 0;
- while (true) {
- while (futureList.size() >= MAX_OUTSTANDING) {
- Thread.sleep(SLEEP_BETWEEN_REAPS);
- reapFutures(futureList);
- }
- // make our job...
- futureList.add(pool.submit(new OurThread()));
- jobC++;
- if (jobC >= NUM_JOBS) {
- break;
- }
- }
- System.err.println("Done submitting.");
- while (futureList.size() > 0) {
- Thread.sleep(SLEEP_BETWEEN_REAPS);
- reapFutures(futureList);
- }
- pool.shutdown();
- System.err.println("Done reaping.");
- }
- private void reapFutures(List<Future<Result>> futureList) {
- Iterator<Future<Result>> iterator = futureList.iterator();
- int reapC = 0;
- while (iterator.hasNext()) {
- Future<Result> future = iterator.next();
- if (!future.isDone() && !future.isCancelled()) {
- continue;
- }
- iterator.remove();
- reapC++;
- Result result;
- try {
- result = future.get();
- } catch (Exception e) {
- e.printStackTrace();
- continue;
- }
- // process the result
- }
- System.err.println("Reaped " + reapC);
- }
- private static class OurThread implements Callable<Result> {
- public Result call() {
- // do something...
- return new Result(/* ... */);
- }
- }
- private static class Result {
- public Result() {
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement