Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package com.paramsen.testground.test_concurrent_download;
- import java.util.ArrayList;
- import java.util.List;
- import java.util.Stack;
- /**
- * Concurrently download from predefined list of urls on a predefined amount of worker threads.
- * Workers execute requests until the Stack is depleted in a synchronized manor, since Stack
- * is synchronized in Java.
- *
- * @author Pär Amsen 05/2017
- */
- public class ConcurrentRequestSource {
- public static final int WORKERS = 4;
- /**
- * @param callback is a callback that will be called with each work result when available
- */
- public void download(Stack<String> urls, Callback<String> callback) {
- newWorkers(urls, callback).forEach(Thread::start);
- }
- private List<Thread> newWorkers(Stack<String> urls, Callback<String> callback) {
- ArrayList<Thread> workers = new ArrayList<>();
- for (int i = 0; i < WORKERS; i++) {
- workers.add(new Thread(newWorker(urls, callback), "ConcurrentRequestSourceWorker-" + i));
- }
- return workers;
- }
- private Runnable newWorker(Stack<String> urls, Callback<String> callback) {
- return () -> {
- while (!urls.empty()) {
- callback.call(doRequest(urls.pop()));
- }
- System.out.println(String.format("Thread %s done", Thread.currentThread().getName()));
- };
- }
- private String doRequest(String url) {
- try {
- Thread.sleep(10); //mock request time
- } catch (InterruptedException e) {
- //ignore
- }
- System.out.printf("Downloaded %s on thread: %s%n", url, Thread.currentThread().getName());
- return "path to download from " + url;
- }
- interface Callback<T> {
- void call(T t);
- }
- }
- /**
- * Unit test
- *
- * @author Pär Amsen 05/2017
- */
- public class ConcurrentRequestSourceTest {
- @Test
- public void download() throws Exception {
- List<String> results = new ArrayList<>();
- Stack<String> data = new Stack<>();
- for (int i = 0; i < 2000; i++) {
- data.add(String.valueOf(i));
- }
- new ConcurrentRequestSource().download(data, results::add);
- while (results.size() < 2000) {
- Thread.sleep(10);
- }
- assertEquals(2000, results.size());
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement