Thread-safe operations?

I’m currently in the process of writing the documentation for the SchedulerService, and as such I was looking to compile a list of all the thread-safe things you can do inside an asynchronous thread. So far, the only parts of Minecraft that are thread-safe that I can find are:

  • Chat

Does anyone know of others?

1 Like

since Minecraft’s singlethreaded, there’s little else you can do with Minecraft on an async thread; if you really need to do work asynchronously you should just do all the parts on the thread that don’t touch the API, and then tell it to register a task with the scheduler to indicate its done or something.

packet sending should be synchronized as well.

I though SpongeAPI uses the javax.annotation.concurrent.ThreadSafe to mark thread-safe methods?

If you are writing the documentation that covers asynchronous execution, it is probably a good idea to also add links to popular concurrency tutorials such as the official tutorial or Java Concurrency in Practice by Brian Goetz.

I can only find one place where it’s used, but it’s a good thought

Coincidentally, @zml and I were chatting about this after SoS V. He mentioned that sponge’s built-in Permissions handling is also thread safe. Hope that helps.

2 Likes
  • Anything that doesn’t use the client<->server connection in any way. Examples:
    • Independent network connections
      • HTTP Web APIs (e.g REST services)
      • Connections to external applications (SQL servers, web servers, a link with another application on a player’s computer, …).
    • Other I/O
      • Saving files (not including files managed by MC/Sponge)
      • Polling for the presents or absence of a file

(Disclamer, I could be completely wrong, but these are things you can do in an async thread)

  • Generally anything that doesn’t require access to Minecraft’s own
    resources is okay.
  • The Client <-> Server connection is partially
    synchronized, so obviously a no go.
  • Any IO (That’s networking, Files,
    SQL, …) is fine, as long as you aren’t aquiring locks on mc’s files, as that can go pretty wrong if not expected

Idk which things in the Sponge API are okay / not okay specifically tho.

Depending on the implementation, event handling looks like it was attempted to be made thread-safe.

I suggest that the thread safety requirements of each class be evaluated and documented by the API authors because synchronization policies differing based on implementation is definitely not a good thing.

2 Likes

I absolutely agree, which is a part of the reason I made this forum post, because there is usually a bit of a divide between those writing the API/Implementation and those writing the docs.

2 Likes