Guest User

Untitled

a guest
Jan 20th, 2018
68
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.55 KB | None | 0 0
  1. import java.io.File;
  2. import java.util.ArrayList;
  3. import java.util.List;
  4. import java.util.Stack;
  5. import java.util.concurrent.atomic.AtomicBoolean;
  6.  
  7. // MIT License
  8. //
  9. // Copyright(c) 2017 Pär Amsen
  10. //
  11. // Permission is hereby granted,free of charge,to any person obtaining a copy
  12. // of this software and associated documentation files(the"Software"),to deal
  13. // in the Software without restriction,including without limitation the rights
  14. // to use,copy,modify,merge,publish,distribute,sublicense,and/or sell
  15. // copies of the Software,and to permit persons to whom the Software is
  16. // furnished to do so,subject to the following conditions:
  17. //
  18. // The above copyright notice and this permission notice shall be included in all
  19. // copies or substantial portions of the Software.
  20. //
  21. // THE SOFTWARE IS PROVIDED"AS IS",WITHOUT WARRANTY OF ANY KIND,EXPRESS OR
  22. // IMPLIED,INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  23. // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.IN NO EVENT SHALL THE
  24. // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,DAMAGES OR OTHER
  25. // LIABILITY,WHETHER IN AN ACTION OF CONTRACT,TORT OR OTHERWISE,ARISING FROM,
  26. // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
  27. // SOFTWARE.
  28.  
  29. /**
  30. * Concurrently download from predefined list of urls on a predefined amount of worker threads.
  31. * Workers execute requests until the Stack is depleted in a synchronized manor, since Stack
  32. * is synchronized in Java.
  33. *
  34. * Does not care about IO errors and/or pause/resume for now
  35. *
  36. * @author Pär Amsen 05/2017
  37. */
  38. public class ConcurrentRequestSource {
  39. public static final int WORKERS = 4;
  40.  
  41. private AtomicBoolean done;
  42. private File imagesDir;
  43. private Stack<String> urls;
  44.  
  45. public ConcurrentRequestSource(File imagesDir, List<String> urls) {
  46. this.imagesDir = imagesDir;
  47. this.urls = new Stack<>();
  48. this.urls.addAll(urls);
  49. done = new AtomicBoolean(false);
  50. }
  51.  
  52. /**
  53. * @param callback is a callback that will be called with each work result when available
  54. */
  55. public void download(Callback callback) {
  56. for(Thread worker : newWorkers(callback)) {
  57. worker.start();
  58. }
  59. }
  60.  
  61. private List<Thread> newWorkers(Callback callback) {
  62. ArrayList<Thread> workers = new ArrayList<>();
  63.  
  64. for (int i = 0; i < WORKERS; i++) {
  65. workers.add(new Thread(newWorker(callback), "ConcurrentRequestSourceWorker-" + i));
  66. }
  67.  
  68. return workers;
  69. }
  70.  
  71. private Runnable newWorker(Callback callback) {
  72. return new Runnable() {
  73. @Override
  74. public void run() {
  75. while (!urls.empty()) {
  76. callback.onEach(ConcurrentRequestSource.this.doDownload(urls.pop()));
  77. }
  78.  
  79. if(done.compareAndSet(false, true)) {
  80. callback.onDone(imagesDir);
  81. }
  82.  
  83. System.out.println(String.format("Thread %s done", Thread.currentThread().getName()));
  84. }
  85. };
  86. }
  87.  
  88. /**
  89. * Download and persist file on disk
  90. *
  91. * @return File pointing to downloaded file on disk
  92. */
  93. private File doDownload(String url) {
  94. try {
  95. Thread.sleep(10); //mock request time
  96. } catch (InterruptedException e) {
  97. //ignore
  98. }
  99.  
  100. System.out.printf("Downloaded %s on thread: %s%n", url, Thread.currentThread().getName());
  101. return null; // file pointing to downloaded image on disk
  102. }
  103.  
  104. interface Callback {
  105. void onEach(File image);
  106. void onDone(File imagesDir);
  107. }
  108. }
Add Comment
Please, Sign In to add comment