Why Spigot isn't thread safe and how I can do good practices while making thread safe api's.

Discussion in 'Spigot Plugin Development' started by Mr.Midnight, May 24, 2016.

  1. Ello, I was wondering why Spigot's api isn't thread safe and how I could go about making a multithreaded safe api?
  2. Don't blame spigot, blame the minecraft server implementation they have to work with. You are free to make your classes thread safe and then any time you need to interact with specific methods in the spigot API that you know aren't thread safe (because some still are, you just have to be really careful with which ones) you can schedule sync task to join back up with the main thread.
    • Informative Informative x 1
  3. Mind if I ask why some of minecraft server's implementation isn't thread safe?
  4. I'm sorry I couldn't tell you :/ Simplicity I guess?
  5. Hm ok it's fine :p
  6. MiniDigger


    ask notch. And blame mojang for not changing that.
    you need to remember that minecraft was created in 2010. Back then, we still used java in our browsers (thats why notch choosed java in the first place)
    I guess multithreading was just not that popular. Idk if multi core cpus where a thing back then
    • Agree Agree x 3
    • Like Like x 1
    • Funny Funny x 1
  7. Minecraft isn't web based tho :p
  8. MiniDigger


    it was back in 2010 tho ;)
    (and for many years after that, remember classic edition on the old minecraft.net website?)
    • Agree Agree x 1
  9. Multicore CPUs have been around for awhile ._.
    • Agree Agree x 1
  10. I wonder how hard it would be to reconstruct the minecraft server implementation, sounds like a fun project.

    Edit: And no :p
  11. MiniDigger


    there are few projects out there http://minecraft.gamepedia.com/Custom_servers#Survival
    the most popular is glowstone (latest version is named growstone++)
    it uses papers api (so it includes spigot and bukkits api)
  12. Spigot isn't on that list XD
  13. MiniDigger


    go ahead and edit the page if you want
  14. Notch wrote Minecraft in Java because that's what he knew. Java was one of languages he knew quite well and could develop in without running into problems.
  15. Bump, let's talk a bit more about good practices. :3
  16. Because making things thread-safe = massive overhead. The general saying when multi-threading is to insure no threads access the same data, that they should be isolated. If you do have multiple threads accessing the same data, you need to synchronize them which of course causes overhead and generally decreases performance.

    The only thing IMO that should be ran in separate threads is Chunk/Other IO. The only issue with doing that is people tend to request data (Such as blocks) from chunks that are not loaded. In that case, i believe spigot should throw a ChunkNotLoadedExecption checked exception when attempting to access chunk related data.
    • Informative Informative x 1
  17. Good to know. But doesn't synchronize only allow one thread at a time?
  18. He means literal synchronization, not the keyword.
  19. Yes synchronizing methods/blocks would queue so to speak threads to insure only one is executing at a time. It sort of defeats the purpose of multi-threading when you're forcing threads to wait for others to finish though lol. I'm not sure if it's still an active issue with modern JVM's, but it used to also be a massive source of overhead and in some benchmarks slowed down methods by up to x50.

    My point is when using multiple threads, you shouldn't be accessing any data that is access by other threads. There are many ways to access the same data and some built in data structures within Java that allow "perfect" mutli-thread access but it's still not a good idea to have threads working against eachother like that.
  20. I don't follow :c.

    Rip multi core processors.