Confused about Async

Discussion in 'Plugin Development' started by amitlin14, Jun 18, 2013.

Thread Status:
Not open for further replies.
  1. Offline

    amitlin14

    Hi there! I have a bit of a question about Aync tasks, for example, i need to update info from a class of mine to a mysql data, almost all of the data i update is from a class i created, and has nothing to do with the bukkit api, i say almost because i use one method which IS from the bukkit api, and the getBalance method from vault.

    Should i be using an Async task to perform these updates? or should i use a Sync task? also, if i just want to update a player's info to the mysql database once, out of the repeating task, when the player first joins, should i use Bukkit.getScheduler().runTaskAsynchronously? or simply just update it without the async task?

    I cant help but feel like i dont quite understand what Async tasks are, if anyone would care to explain just what they are and when should they be used, that would be great.
     
  2. Async threads run in parallel/at the same time as the main game loop.
    This is good if you need to do something that is slow (like say, network IO to a database on another machine). if it was done sync (same thread as the game loop) the game would stop responding unti this was done.

    With saving the data, I would recommend a "save record"/caching. You basically want to pass to the async thread only data and have the async thread just call the db to update/insert data, no method calling to get the data.

    Example based off of BeardStat's Save system.

    you have sync task run every x minutes, it creates "records" that the database should save.
    Code:
    //hacky code, example only pls don't do this way
    public class Record{
    public String player;
    public double balance;
    public boolean isCool;
    }
    
    The sync task basically collates the data, then calls bukkit to run an async task on that data.

    Code:
    ...
    public void run(){
     
    for(Record r : records){
    // SQL DATABASE STUFF ONLY HERE
    }
     
    }
    
    As for loading async, that's a whole can of worms you really don't wanna touch unless you absolutely have to, most people are rocking there SQL database on the same machine/local network as the game server, so a "select" query during join shouldn't cause problems.
     
  3. Offline

    amitlin14

    tehbeard that is exactly how im doing it, but im also using some bukkit api in it, like OfflinePlayer, Player and using the isOnline method, so should i be using the async task, or is there any way around it?
     
  4. Offline

    lycano

    amitlin14 create a container for the PlayerData you need to pass to your async and this should not be a problem. Using Player object and checking for isOnline inside an async Task could be a problem. You can easily avoid it by only passing the data to this task as tehbeard mentioned in his example.
     
Thread Status:
Not open for further replies.

Share This Page