Solved Thread Safety

Discussion in 'Plugin Development' started by AlvinB, Dec 19, 2016.

Thread Status:
Not open for further replies.
  1. So, I have come to be very confused about Thread Safety and bukkit. From my knowledge of thread safety, pretty much every single method in bukkit (with the exception of schedulers) is non-thread safe, yet I see lots of developers using bukkit's and their own (non-threadsafe) methods in async tasks and asynchronous events. Now, if this was just people with little java experience, I could understand, but when plugins with upwards of a quarter million downloads just show complete disregard of thread-safety I start to wonder. Have I gotten the whole concept of thread safety wrong? So my question is, am I right in saying that most of bukkit's methods are non thread-safe, and if so, why is everyone disregarding it?
     
  2. Offline

    Tecno_Wizard

    @AlvinB there are some things that are safe that wouldn't appear to be. What function was being called? A lot of the messaging systems in Bukkit are actually thread safe, but aren't documented as so.
     
  3. Offline

    mythbusterma

    @AlvinB

    It's true, and thread safety is very difficult. In addition, it may never manifest as an issue. I'm not surprised. Towny is some of the worst code I've ever seen in my life, so it seems pretty average for these plugins.

    Most are not thread safe, only the ones that are listed can be counted on to be thread safe, although Spigot may be better about it than CraftBukkit.
     
    bwfcwalshy likes this.
  4. @mythbusterma @Tecno_Wizard
    Okay, so I'm not crazy. But why do people not just schedule a sync task to be sure? Are there even any downsides to scheduling a sync task in this case (besides the 1 tick delay)?
     
  5. Offline

    timtower Administrator Administrator Moderator

    @AlvinB Maybe you are a little bit :p
    I think that a lot of developers don't know that the issue exists and can mess up your plugin.
    The only thing that I can think of besides that 1 tick delay is memory, but if this memory difference is noticeable then you are doing something very wrong.
     
    AlvinB likes this.
  6. @timtower
    Yeah, I was messing with AsyncPlayerChatEvent and this just kinda dawned on me. I guess the tutorials have to do a better job of telling people this, since the fix is so simple! I'll call this thread solved.
     
  7. Offline

    mythbusterma

    @AlvinB

    Well the sync tasks can take the server main thread's time. By doing it asynchronously, you will use another thread's time, so the server doesn't lag because of what you're doing. This is especially important for networking things.
     
  8. @mythbusterma
    Yeah, I understand with these edge cases (networking, IO, etc) but for things to do with the actual server, I should use a sync task. Am I correct?
     
    DoggyCodeâ„¢ and timtower like this.
  9. Offline

    mythbusterma

    @AlvinB

    I wouldn't call those "edge cases," they're relatively common things to do, and there's nothing particularly unusual about them that those would be something that is outside the norm.

    Anything that actually does work on the server (entities, players, blocks, etc.) should definitely be synchronous. That doesn't mean it has to be entirely synchronous, however. For example, I have a plugin that would load a schematic and compute a plan on another thread, then execute the plan over multiple ticks on the main thread.
     
  10. Offline

    Tecno_Wizard

    @AlvinB a lot of my plugins work like this. They will prepare the information they need to be done on the main thread asynchronously and the actual work will be done synchronously once the other pieces are ready. (i.e. inventory GUIs that need some file IO to load it's data)
     
Thread Status:
Not open for further replies.

Share This Page