Skip to content

Instantly share code, notes, and snippets.

@blablubbabc
Last active January 15, 2022 14:58
Show Gist options
  • Save blablubbabc/e884c114484f34cae316c48290b21d8e to your computer and use it in GitHub Desktop.
Save blablubbabc/e884c114484f34cae316c48290b21d8e to your computer and use it in GitHub Desktop.

Revisions

  1. blablubbabc revised this gist Jan 15, 2022. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion SomePlugin.java
    Original file line number Diff line number Diff line change
    @@ -9,7 +9,7 @@ public void onDisable() {
    boolean asyncTasksTimeout = false;
    while (this.getActiveAsyncTasks() > 0) {
    try {
    Thread.sleep(5);
    Thread.sleep(25);
    } catch (InterruptedException e) {
    e.printStackTrace();
    }
  2. blablubbabc renamed this gist May 12, 2016. 1 changed file with 13 additions and 13 deletions.
    26 changes: 13 additions & 13 deletions gistfile1.txt → SomePlugin.java
    Original file line number Diff line number Diff line change
    @@ -1,4 +1,4 @@
    # inside plugin class
    // inside the plugin class
    private static final long ASYNC_TASKS_TIMEOUT_SECONDS = 10;

    @Override
    @@ -9,21 +9,21 @@ public void onDisable() {
    boolean asyncTasksTimeout = false;
    while (this.getActiveAsyncTasks() > 0) {
    try {
    Thread.sleep(5);
    } catch (InterruptedException e) {
    e.printStackTrace();
    }
    // after timeout disable anyways..:
    if (System.currentTimeMillis() - asyncTasksStart > asyncTasksTimeoutMillis) {
    asyncTasksTimeout = true;
    this.getLogger().warning("Waited " + ASYNC_TASKS_TIMEOUT_SECONDS + " seconds for " + this.getActiveAsyncTasks()
    + " remaining async tasks to complete. Disabling now anyways..");
    break;
    }
    Thread.sleep(5);
    } catch (InterruptedException e) {
    e.printStackTrace();
    }
    // after timeout disable anyways..:
    if (System.currentTimeMillis() - asyncTasksStart > asyncTasksTimeoutMillis) {
    asyncTasksTimeout = true;
    this.getLogger().warning("Waited " + ASYNC_TASKS_TIMEOUT_SECONDS + " seconds for " + this.getActiveAsyncTasks()
    + " remaining async tasks to complete. Disabling now anyways..");
    break;
    }
    }
    final long asyncTasksTimeWaited = System.currentTimeMillis() - asyncTasksStart;
    if (!asyncTasksTimeout && asyncTasksTimeWaited > 1) {
    this.getLogger().info("Waited " + asyncTasksTimeWaited + " ms for async tasks to finish.");
    this.getLogger().info("Waited " + asyncTasksTimeWaited + " ms for async tasks to finish.");
    }
    }

  3. blablubbabc created this gist May 12, 2016.
    38 changes: 38 additions & 0 deletions gistfile1.txt
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,38 @@
    # inside plugin class
    private static final long ASYNC_TASKS_TIMEOUT_SECONDS = 10;

    @Override
    public void onDisable() {
    // wait for async tasks to complete:
    final long asyncTasksTimeoutMillis = ASYNC_TASKS_TIMEOUT_SECONDS * 1000;
    final long asyncTasksStart = System.currentTimeMillis();
    boolean asyncTasksTimeout = false;
    while (this.getActiveAsyncTasks() > 0) {
    try {
    Thread.sleep(5);
    } catch (InterruptedException e) {
    e.printStackTrace();
    }
    // after timeout disable anyways..:
    if (System.currentTimeMillis() - asyncTasksStart > asyncTasksTimeoutMillis) {
    asyncTasksTimeout = true;
    this.getLogger().warning("Waited " + ASYNC_TASKS_TIMEOUT_SECONDS + " seconds for " + this.getActiveAsyncTasks()
    + " remaining async tasks to complete. Disabling now anyways..");
    break;
    }
    }
    final long asyncTasksTimeWaited = System.currentTimeMillis() - asyncTasksStart;
    if (!asyncTasksTimeout && asyncTasksTimeWaited > 1) {
    this.getLogger().info("Waited " + asyncTasksTimeWaited + " ms for async tasks to finish.");
    }
    }

    private int getActiveAsyncTasks() {
    int workers = 0;
    for (BukkitWorker worker : Bukkit.getScheduler().getActiveWorkers()) {
    if (worker.getOwner().equals(this)) {
    workers++;
    }
    }
    return workers;
    }