Changeset 31472 in osm


Ignore:
Timestamp:
2015-08-10T12:41:58+02:00 (9 years ago)
Author:
nokutu
Message:

Fixed concurrency bug when deleting the layer during a download. It would reopen itself.

Location:
applications/editors/josm/plugins/mapillary/src/org/openstreetmap/josm/plugins/mapillary/downloads
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • applications/editors/josm/plugins/mapillary/src/org/openstreetmap/josm/plugins/mapillary/downloads/MapillaryDownloader.java

    r31460 r31472  
    33import static org.openstreetmap.josm.tools.I18n.tr;
    44
     5import java.util.ArrayList;
    56import java.util.concurrent.ArrayBlockingQueue;
    67import java.util.concurrent.ConcurrentHashMap;
     
    3637  public static final int MANUAL = 2;
    3738
     39  /** All the Threads that have been run. Used to interrupt them properly. */
     40  private static ArrayList<Thread> threads = new ArrayList<>();
     41
    3842  /** Max area to be downloaded */
    3943  public static final double MAX_AREA = Main.pref.getDouble(
     
    4549  public final static String CLIENT_ID = "T1Fzd20xZjdtR0s1VDk5OFNIOXpYdzoxNDYyOGRkYzUyYTFiMzgz";
    4650  /** Executor that will run the petitions. */
    47   private static ThreadPoolExecutor EXECUTOR;
     51  private static ThreadPoolExecutor EXECUTOR = new ThreadPoolExecutor(3, 5,
     52      100, TimeUnit.SECONDS, new ArrayBlockingQueue<Runnable>(50));
    4853
    4954  /**
     
    6267    queryStringParts.put("max_lat", maxLatLon.lat());
    6368    queryStringParts.put("max_lon", maxLatLon.lon());
    64     EXECUTOR = new ThreadPoolExecutor(3, 5, 100, TimeUnit.SECONDS,
    65         new ArrayBlockingQueue<Runnable>(50));
    6669    run(new MapillarySquareDownloadManagerThread(queryStringParts));
    6770  }
    6871
    6972  private static void run(Thread t) {
     73    threads.add(t);
    7074    EXECUTOR.execute(t);
    7175  }
     
    215219   */
    216220  public static void stopAll() {
     221    for (Thread t : threads) {
     222      if (t.isAlive())
     223        System.out.println(t);
     224      t.interrupt();
     225    }
     226    threads.clear();
    217227    EXECUTOR.shutdownNow();
     228    try {
     229      EXECUTOR.awaitTermination(30, TimeUnit.SECONDS);
     230    } catch (InterruptedException e) {
     231      // TODO Auto-generated catch block
     232      e.printStackTrace();
     233    }
    218234    EXECUTOR = new ThreadPoolExecutor(3, 5, 100, TimeUnit.SECONDS,
    219235        new ArrayBlockingQueue<Runnable>(50));
  • applications/editors/josm/plugins/mapillary/src/org/openstreetmap/josm/plugins/mapillary/downloads/MapillarySquareDownloadManagerThread.java

    r31456 r31472  
    3030  private final String sequenceQueryString;
    3131  private final String signQueryString;
     32
     33  private ThreadPoolExecutor downloadExecutor = new ThreadPoolExecutor(3, 5,
     34      25, TimeUnit.SECONDS, new ArrayBlockingQueue<Runnable>(5));
     35  private ThreadPoolExecutor completeExecutor = new ThreadPoolExecutor(3, 5,
     36      25, TimeUnit.SECONDS, new ArrayBlockingQueue<Runnable>(5));
     37  private ThreadPoolExecutor signsExecutor = new ThreadPoolExecutor(3, 5, 25,
     38      TimeUnit.SECONDS, new ArrayBlockingQueue<Runnable>(5));
    3239
    3340  /**
     
    7885    } finally {
    7986      PluginState.finishDownload();
    80       MapillaryUtils.updateHelpText();
    8187    }
    8288    MapillaryUtils.updateHelpText();
     
    8793
    8894  private void downloadSequences() throws InterruptedException {
    89     ThreadPoolExecutor ex = new ThreadPoolExecutor(3, 5, 25, TimeUnit.SECONDS,
    90         new ArrayBlockingQueue<Runnable>(5));
    9195    int page = 0;
    92     while (!ex.isShutdown()) {
    93       ex.execute(new MapillarySequenceDownloadThread(ex,
    94           this.sequenceQueryString + "&page=" + page + "&limit=10"));
    95       while (ex.getQueue().remainingCapacity() == 0)
     96    while (!this.downloadExecutor.isShutdown()) {
     97      this.downloadExecutor.execute(new MapillarySequenceDownloadThread(
     98          this.downloadExecutor, this.sequenceQueryString + "&page=" + page
     99              + "&limit=10"));
     100      while (this.downloadExecutor.getQueue().remainingCapacity() == 0)
    96101        Thread.sleep(500);
    97102      page++;
    98103    }
    99     ex.awaitTermination(15, TimeUnit.SECONDS);
     104    this.downloadExecutor.awaitTermination(15, TimeUnit.SECONDS);
    100105    MapillaryData.dataUpdated();
    101106  }
    102107
    103108  private void completeImages() throws InterruptedException {
    104     ThreadPoolExecutor ex = new ThreadPoolExecutor(3, 5, 25, TimeUnit.SECONDS,
    105         new ArrayBlockingQueue<Runnable>(5));
    106109    int page = 0;
    107     while (!ex.isShutdown()) {
    108       ex.execute(new MapillaryImageInfoDownloaderThread(ex,
    109           this.imageQueryString + "&page=" + page + "&limit=20"));
    110       while (ex.getQueue().remainingCapacity() == 0)
     110    while (!this.completeExecutor.isShutdown()) {
     111      this.completeExecutor.execute(new MapillaryImageInfoDownloaderThread(
     112          this.completeExecutor, this.imageQueryString + "&page=" + page
     113              + "&limit=20"));
     114      while (this.completeExecutor.getQueue().remainingCapacity() == 0)
    111115        Thread.sleep(100);
    112116      page++;
    113117    }
    114     ex.awaitTermination(15, TimeUnit.SECONDS);
     118    this.completeExecutor.awaitTermination(15, TimeUnit.SECONDS);
    115119  }
    116120
    117121  private void downloadSigns() throws InterruptedException {
    118     ThreadPoolExecutor ex = new ThreadPoolExecutor(3, 5, 25, TimeUnit.SECONDS,
    119         new ArrayBlockingQueue<Runnable>(5));
    120122    int page = 0;
    121     while (!ex.isShutdown()) {
    122       ex.execute(new MapillaryTrafficSignDownloaderThread(ex,
    123           this.signQueryString + "&page=" + page + "&limit=20"));
    124       while (ex.getQueue().remainingCapacity() == 0)
     123    while (!this.signsExecutor.isShutdown()) {
     124      this.signsExecutor.execute(new MapillaryTrafficSignDownloaderThread(
     125          this.signsExecutor, this.signQueryString + "&page=" + page
     126              + "&limit=20"));
     127      while (this.signsExecutor.getQueue().remainingCapacity() == 0)
    125128        Thread.sleep(100);
    126129      page++;
    127130    }
    128     ex.awaitTermination(15, TimeUnit.SECONDS);
     131    this.signsExecutor.awaitTermination(15, TimeUnit.SECONDS);
     132  }
     133
     134  @Override
     135  public void interrupt() {
     136    super.interrupt();
     137    this.downloadExecutor.shutdownNow();
     138    this.completeExecutor.shutdownNow();
     139    this.signsExecutor.shutdownNow();
     140    try {
     141      this.downloadExecutor.awaitTermination(15, TimeUnit.SECONDS);
     142      this.completeExecutor.awaitTermination(15, TimeUnit.SECONDS);
     143      this.signsExecutor.awaitTermination(15, TimeUnit.SECONDS);
     144    } catch (InterruptedException e) {
     145      Main.error(e);
     146    }
    129147  }
    130148}
Note: See TracChangeset for help on using the changeset viewer.