Advertisement
ThinMatrix

Loader Thread Example

Jan 17th, 2016
252
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 0.95 KB | None | 0 0
  1. import java.util.ArrayList;
  2. import java.util.List;
  3.  
  4. public class LoaderThread extends Thread {
  5.  
  6.     private boolean running = true;
  7.     private List<ModelOrSomething> waitingResources = new ArrayList<ModelOrSomething>();
  8.  
  9.     public LoaderThread() {
  10.         this.start();
  11.     }
  12.  
  13.     public synchronized void addResourceToLoad(ModelOrSomething resource) {
  14.         waitingResources.add(resource);
  15.     }
  16.    
  17.     public void stopThread(){
  18.         running = false;
  19.     }
  20.  
  21.     @Override
  22.     public void run() {
  23.         while (running) {
  24.             List<ModelOrSomething> resources = getResources();
  25.             for (ModelOrSomething resource : resources) {
  26.                 resource.load();
  27.             }
  28.             try {
  29.                 sleep(10);
  30.             } catch (InterruptedException e) {
  31.                 e.printStackTrace();
  32.             }
  33.         }
  34.     }
  35.  
  36.     private synchronized List<ModelOrSomething> getResources() {
  37.         List<ModelOrSomething> resourcesForLoading = new ArrayList<ModelOrSomething>(waitingResources);
  38.         waitingResources.clear();
  39.         return resourcesForLoading;
  40.     }
  41.  
  42. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement