Async tasks executed in order?

Discussion in 'Plugin Development' started by ZephireNZ, Mar 24, 2014.

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

    ZephireNZ

    I'm making a plugin that uses a database as a backend. To avoid major lag, I've made all changes to the database use an async task to execute in the background.

    However, I'm worried that this could cause some problems. Take this snippet, for example:
    Code:java
    1. public void updateDb(int value) {
    2. db.updateAsync(value); // Creates and schedules an async task for the next tick.
    3. }
    4. ...
    5. updateDb(5);
    6. updateDb(4);
    7. updateDb(7);


    I'm worried that the above code might not execute in the order that was scheduled, resulting in, say, the database receiving "4,7,5".

    So my question is, are Async tasks scheduled for the same tick executed in the order they were scheduled (eg first goes first), or is it random/a stack (first goes last)?
     
  2. Offline

    amhokies

    ZephireNZ
    Using this code: http://hastebin.com/jiciliwuqe.java

    I got this output: http://hastebin.com/dapahutama.vbs

    So, it seems that it does act as a stack. (With tasks scheduled to be run on the same tick)

    EDIT: Strange thing is, though, the first task scheduled always runs first.

    EDIT #2: Tried the same method, but this time with async tasks, and they didn't run in any particular order. They were basically all over the place.
     
  3. Offline

    ZephireNZ

    Well, it's a good thing I asked first!

    Maybe it's the way they're stored? Using a stack and a hashmap respectively could give those results.
     
  4. Offline

    Rocoty

    May I suggest you perform all these database operations in the same thread then? E.g only e scheduling one task
     
Thread Status:
Not open for further replies.

Share This Page