Created
July 7, 2017 04:05
-
-
Save stajkowski/25794df1ee8d7d65dff01519044129ab to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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