Multi-Threading MySQL/HTTP Operations

I do the following after I run that code:

  • Send the player/CommandSource chat messages
  • File I/O with Configurate
  • Database reading and editing
  • Use the game object to post a custom event
  • Print messages using the logger

Have you tried to call your custom event in a another thread??

It seems to work fine, I am not sure if it is thread-safe though.

If you want to check if its thread safe, call your custom event like 1000 times in a another thread. If it dont crash, its probably thread safe.

I called the custom event 1 billion times twice and nothing happened.

Is this a reliable way to guarantee that something is thread-safe? - @Zirconium

I believe everything in that list you have is thread-safe, with the exception of events (I think)… Most of the Sponge events should be getting called on the main thread as they all directly relate to something in the game…

Did you call them synchronously or asynchronously?

@Zirconium
I posted the custom event I created asynchronously

And when listening for the event, you didn’t get any hits on your event listener?

@Zirconium
Each time it posted successfully. Everything worked perfectly, nothing out of the ordinary.

I suppose its a safe bet to say it’s thread-safe. In that case, you could do your entire operation inside an asynchronous Runnable

The event might get fired from another thread, but the listener is on the main thread, no?

@wingz
Yes the event is fired from a different thread, but it can be listened to on the main thread.

@Zirconium
Ok, thanks.

There is two way the listener can receive the event:

First:

  • When you call your event, the server will call the event in the thread you called the event.

Second:

  • When you call your event, the server will store the event called in a list or something and he will call it in the main thread.

It depends how the event system is programmed.

Please learn more of the SpongeAPI. You should not be making direct HTTP calls (edit: calls to Mojang’s APIs).
There is a service for that.

public UUID getUUID(String name) {
    GameProfileResolver resolver = game.getServiceManager().provide(GameProfileResolver.class).get();
    GameProfile profile resolver.get(name).get();
    return profile.getUniqueId();
}

@simon816
Ok, thanks. If I shouldn’t make direct HTTP calls, do you have any recommendations for reading data of a GitHub web page?

1 Like

I meant for Mojang’s APIs. You can use Java’s HTTP library for other websites. Although who knows, maybe SpongeAPI will have a HTTP request/RESTful service in the future.

1 Like

Ahh, ok thanks for the information!

1 Like

Please no? That’s kinda out of scope.

Well there was one commit…
https://github.com/SpongePowered/SpongeAPI/commit/f90435e4739

I think the API won’t go into web services though.

Well, I guess I’ll just watch that closely.

1 Like