DownloadThreadPool.java 1.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556
  1. package com.rarchives.ripme.ripper;
  2. import java.util.concurrent.Executors;
  3. import java.util.concurrent.ThreadPoolExecutor;
  4. import java.util.concurrent.TimeUnit;
  5. import com.rarchives.ripme.utils.Utils;
  6. import org.apache.logging.log4j.LogManager;
  7. import org.apache.logging.log4j.Logger;
  8. /**
  9. * Simple wrapper around a FixedThreadPool.
  10. */
  11. public class DownloadThreadPool {
  12. private static final Logger logger = LogManager.getLogger(DownloadThreadPool.class);
  13. private ThreadPoolExecutor threadPool = null;
  14. public DownloadThreadPool() {
  15. initialize("Main");
  16. }
  17. public DownloadThreadPool(String threadPoolName) {
  18. initialize(threadPoolName);
  19. }
  20. /**
  21. * Initializes the threadpool.
  22. * @param threadPoolName Name of the threadpool.
  23. */
  24. private void initialize(String threadPoolName) {
  25. int threads = Utils.getConfigInteger("threads.size", 10);
  26. logger.debug("Initializing " + threadPoolName + " thread pool with " + threads + " threads");
  27. threadPool = (ThreadPoolExecutor) Executors.newFixedThreadPool(threads);
  28. }
  29. /**
  30. * For adding threads to execution pool.
  31. * @param t
  32. * Thread to be added.
  33. */
  34. public void addThread(Runnable t) {
  35. threadPool.execute(t);
  36. }
  37. /**
  38. * Tries to shutdown threadpool.
  39. */
  40. public void waitForThreads() {
  41. threadPool.shutdown();
  42. try {
  43. threadPool.awaitTermination(3600, TimeUnit.SECONDS);
  44. } catch (InterruptedException e) {
  45. logger.error("[!] Interrupted while waiting for threads to finish: ", e);
  46. }
  47. }
  48. }