A Sync or not to A Sync

Discussion in 'Plugin Development' started by Ne0nx3r0, Apr 17, 2011.

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

    Ne0nx3r0

    For small tasks, should I be Using Sync or Async tasks with the scheduler? Is there a better method than just passing runnables to the scheduler (aka my own timer or something).

    I'm just wondering, I understand the impacts of using sync/async with remote calls and client side coding, but I'm not sure how this translates to server side operations.
     
  2. Offline

    petteyg359

    Code:
    broadcastMessage("This is 0 ticks.");
    sync(doThing(), 20);
    broadcastMessage("Thing done. This is 20 ticks.");
    
    Code:
    broadcastMessage("This is 0 ticks.");
    async(doThing(), 20);
    broadcastMessage("Thing will be done in 20 ticks. This is still 0 ticks.");
    
    Sync executes wherever you put it, and your program sits there and waits until it is done to go to the next line. Async throws it off to be done later while your program continues.


    Why would you implement your own scheduler, when there's already one waiting to be used?
     
  3. Offline

    Drakia

    @petteyg359 Erm, that's not how it works. Neither of them halt execution. The syncschedule call makes it run in the main thread in 20 ticks, while the asyncschedule makes it run in it's own thread in 20 ticks. In both examples however that second broadcastMessage will be called instantly.
     
  4. Offline

    Afforess

    Async whereever possible, Sync whenever needed.

    Edit:

    @Drakia is right - mostly. Neither call will halt execution, immediately. Sync or Async will execute the next tick, if no delay is given. But sync will halt the thread when it runs during the next scheduled tick.
     
  5. Offline

    petteyg359

    I only have used async; I had asked the behavior of sync on IRC and someone apparently misinformed me :)
     
  6. Offline

    Ne0nx3r0

    I've read a few threads nagging it; so I was curious if there was a better alternative.

    When would it be needed?

    I was playing around with scheduling tonight, and I couldn't really spot a difference in my small scheduled stuffs, but while trying to change a block testing with someone else I did basically freeze the server after a TickNextTick error while using Async. (below if anyone is interested in seeing it)

    It only happened once, I couldn't seem to duplicate it.

    Show Spoiler
    2011-04-17 21:31:50 [SEVERE] at net.minecraft.server.World.a(World.java:1485)
    2011-04-17 21:31:50 [SEVERE] at net.minecraft.server.World.h(World.java:1405)
    2011-04-17 21:31:50 [SEVERE] at net.minecraft.server.MinecraftServer.h(MinecraftServer.java:359)
    2011-04-17 21:31:50 [SEVERE] at net.minecraft.server.MinecraftServer.run(MinecraftServer.java:283)
    2011-04-17 21:31:50 [SEVERE] at net.minecraft.server.ThreadServerApplication.run(SourceFile:375)
    2011-04-17 21:31:50 [SEVERE] Unexpected exception
    java.lang.IllegalStateException: TickNextTick list out of synch
    at net.minecraft.server.World.a(World.java:1485)
    at net.minecraft.server.World.h(World.java:1405)
    at net.minecraft.server.MinecraftServer.h(MinecraftServer.java:359)
    at net.minecraft.server.MinecraftServer.run(MinecraftServer.java:283)
    at net.minecraft.server.ThreadServerApplication.run(SourceFile:375)
     
  7. Offline

    Drakia

    @ne0nx3r0 My personal recommendation is avoid async at all costs, there's no guarantee of thread-safety in the Bukkit API calls (As you saw with the TickNextTick error)
     
  8. Offline

    SpaceManiac

    Sync unless you know Async is safe. Any calls to Bukkit in an async callback will randomly cause errors randing from tick list corruption to invalid chunk references to NPEs deep within Minecraft's own source code.
     
  9. Offline

    Sammy

    Can you give me an example of a safe thing to do with Async ? I always wondered ^^
     
  10. Offline

    Afforess

    Saving files with I/O operations, doing math, etc.

    You can always make bukkit API calls before using the aysnc calls, and pass the data to the runnable.

    But again, async whenever possible, sync wherever needed. Async is better, as it won't stop the server, but it's not safe to do it in very many cases.
     
  11. Offline

    Sammy

    @Afforess
    Thanks for the clarification :)
     
  12. Offline

    Ne0nx3r0

    Poor async, you're trying so hard to defend it, heh.

    Very interesting conversation though, I've wondered for a while about async/sync, and this thread clears it up nicely - thanks for the info everyone.
     
Thread Status:
Not open for further replies.

Share This Page