2014-02-27 04:54:44 +01:00
|
|
|
package com.rarchives.ripme.ripper;
|
|
|
|
|
|
|
|
import java.util.concurrent.Executors;
|
2014-05-17 05:21:40 +02:00
|
|
|
import java.util.concurrent.ThreadPoolExecutor;
|
2014-02-27 04:54:44 +01:00
|
|
|
import java.util.concurrent.TimeUnit;
|
|
|
|
|
|
|
|
import org.apache.log4j.Logger;
|
|
|
|
|
|
|
|
import com.rarchives.ripme.utils.Utils;
|
|
|
|
|
2014-03-09 09:20:22 +01:00
|
|
|
/**
|
|
|
|
* Simple wrapper around a FixedThreadPool.
|
|
|
|
*/
|
2014-02-27 04:54:44 +01:00
|
|
|
public class DownloadThreadPool {
|
|
|
|
|
|
|
|
private static final Logger logger = Logger.getLogger(DownloadThreadPool.class);
|
2014-05-17 05:21:40 +02:00
|
|
|
private ThreadPoolExecutor threadPool = null;
|
2014-02-27 04:54:44 +01:00
|
|
|
|
|
|
|
public DownloadThreadPool() {
|
2014-03-07 08:41:49 +01:00
|
|
|
initialize("Main");
|
|
|
|
}
|
2017-05-10 00:22:55 +02:00
|
|
|
|
2014-03-07 08:41:49 +01:00
|
|
|
public DownloadThreadPool(String threadPoolName) {
|
|
|
|
initialize(threadPoolName);
|
|
|
|
}
|
2017-12-28 06:04:23 +01:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Initializes the threadpool.
|
|
|
|
* @param threadPoolName Name of the threadpool.
|
|
|
|
*/
|
2014-03-07 08:41:49 +01:00
|
|
|
private void initialize(String threadPoolName) {
|
2014-02-27 04:54:44 +01:00
|
|
|
int threads = Utils.getConfigInteger("threads.size", 10);
|
2014-03-07 08:41:49 +01:00
|
|
|
logger.debug("Initializing " + threadPoolName + " thread pool with " + threads + " threads");
|
2014-05-17 05:21:40 +02:00
|
|
|
threadPool = (ThreadPoolExecutor) Executors.newFixedThreadPool(threads);
|
2014-02-27 04:54:44 +01:00
|
|
|
}
|
2017-12-28 06:04:23 +01:00
|
|
|
/**
|
|
|
|
* For adding threads to execution pool.
|
|
|
|
* @param t
|
|
|
|
* Thread to be added.
|
|
|
|
*/
|
2014-02-27 04:54:44 +01:00
|
|
|
public void addThread(Thread t) {
|
|
|
|
threadPool.execute(t);
|
|
|
|
}
|
2014-03-10 01:12:10 +01:00
|
|
|
|
2017-12-28 06:04:23 +01:00
|
|
|
/**
|
|
|
|
* Tries to shutdown threadpool.
|
|
|
|
*/
|
2014-02-27 04:54:44 +01:00
|
|
|
public void waitForThreads() {
|
|
|
|
threadPool.shutdown();
|
|
|
|
try {
|
2014-05-17 05:21:40 +02:00
|
|
|
threadPool.awaitTermination(3600, TimeUnit.SECONDS);
|
2014-02-27 04:54:44 +01:00
|
|
|
} catch (InterruptedException e) {
|
2014-03-09 09:20:22 +01:00
|
|
|
logger.error("[!] Interrupted while waiting for threads to finish: ", e);
|
2014-02-27 04:54:44 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|