Running tasks Asynchronously

Discussion in 'Spigot Plugin Development' started by Swedz, Jun 14, 2016.

  1. I've heard that running MySQL queries async is much more reliable and speedy, and also enhances server performance. However, I don't know how to do this..

    Could someone give me a clear example of how to run Asynchronous tasks?

    EDIT: I'm going to bed now, I'll reply within the next 10-12 hours.

    Thanks in advance! :)
     
  2. Code (Text):
    Bukkit.getScheduler().runTaskAsynchronously
     
  3. Well then, that was quick! I guess I'll implement this then go to bed. Thanks :p
     
    • Funny Funny x 1
  4. I'm using this in a static method, how would I use the plugin argument in the static method since it's not possible to directly use 'this' in a static method.
     
  5. Pass the plugin in as an argument
     
  6. dont use statics, create a getter of the class in the main class then create a constructor in the other class your in to get the main class then you can return the instance of it.
     
  7. Like save 'this' as a variable and just put the variable in the plugin argument for runTaskAsynchronously()? Sorry if I'm not understanding right, I'm quite tired :p

    ~ As of right now, I am going to bed, I will come back here in the morning :) ~
     
  8. That is right, yes. Like
    private static Main instance;
    and in onEnable
    instance = this;
     
  9. I would pass the plugin instance as an argument to the static method.
    i.e.
    Code (Text):

    public static void updateSQL(JavaPlugin instance) {
        new BukkitRunnable() {
            @Override
            public void run() {
                //whatever
            }

        }.runTaskAsynchronously(instance);
    }
     
    Then to call it:
    Code (Text):

    updateSQL(this);
     
     
  10. No, it won't make the query any more reliable or any quicker than it is, what it will do, is stop the main thread being blocked.

    The main thread is the server ticking thread, and blocking this is not advised. Nearly everything important (mobs, chunks, redstone and a few other things) are running on the main thread and to block this stops the main thread from going any further until your long task is complete.
     
  11. By quicker I meant if it's doing a query, it wont slow down the main thread at all. Not the query in general being quicker :p