You can make code run asynchronously from the main thread using runTaskAsynchronously
. This is useful for doing intensive math or database operations, as they will prevent the main thread from freezing (and the server from lagging).
Few Bukkit API methods are thread-safe, so many will cause undefined behavior if called asynchronously from the main thread.
Bukkit.getScheduler().runTaskAsynchronously(plugin, new Runnable() {
@Override
public void run() {
Bukkit.getLogger().info("This message was printed to the console asynchronously");
//Bukkit.broadcastMessage is not thread-safe
}
});