Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save stajkowski/25794df1ee8d7d65dff01519044129ab to your computer and use it in GitHub Desktop.
Save stajkowski/25794df1ee8d7d65dff01519044129ab to your computer and use it in GitHub Desktop.
import java.util.concurrent.BlockingQueue;
import java.util.concurrent.ArrayBlockingQueue;
import java.util.HashMap;
public class TestMain {
public static void main(String args[]) {
BlockingQueue<Runnable> bq = new ArrayBlockingQueue<Runnable>(100);
ExecTest exec = new ExecTest(10, bq);
for (int i = 0; i < 10; i++) {
HashMap<String, String> hmap = new HashMap<String, String>();
hmap.put("JobKey1id" + i, "TestValue1id" + i);
hmap.put("JobKey2id" + i, "TestValue2id" + i);
try{
bq.put(new Consumer(new Job(hmap)));
} catch (Exception e) {
System.out.println("Invalid job information: " + e);
continue;
}
}
exec.shutdown();
}
}
import java.util.concurrent.BlockingQueue;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;
import java.util.HashMap;
public class ExecTest {
private final ThreadPoolExecutor exec;
private final BlockingQueue<Runnable> bq;
public ExecTest(int poolSize, BlockingQueue<Runnable> workQueue) {
bq = workQueue;
exec = new ThreadPoolExecutor(poolSize / 2, poolSize, 5, TimeUnit.SECONDS, bq);
exec.prestartAllCoreThreads();
}
public void shutdown(){
exec.shutdown();
}
}
public class Consumer implements Runnable {
private final Job jobData;
public Consumer(Job jobObj) {
jobData = jobObj;
}
public void run() {
jobData.displayJobData();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment