Questions about ExecutorService

Discussion in 'Spigot Plugin Development' started by lurionhk, Nov 17, 2019.

  1. Hey guys, I was discussing with a friend of mine and I ended having some questions:

    1st: Is BukkitRunnable better than ExecutorService? (on performance?)
    2nd: Does ExecutorService finish a thread after executing it or just let it in waiting mode?
    3rd: Do I need to use a ExecutorService when I use HikariCP? (like this code below)

    Code (Java):
    @Override
        public void insert(Storable storable, boolean async) {
            Runnable runnable = () -> {
                try (Connection connection = dataSource.getConnection()){
                    PreparedStatement preparedStatement = connection.prepareStatement("INSERT INTO `"+tableName+"`(`key`, `json`) VALUES (?, ?) ON DUPLICATE KEY UPDATE `json` = VALUES(`json`)");
                    preparedStatement.setString(1, storable.getName());
                    preparedStatement.setString(2, gson.toJson(storable));
                    preparedStatement.executeUpdate();
                } catch (SQLException e) {
                    e.printStackTrace();
                }
            };

            if (async) executor.submit(runnable);
            else runnable.run();
        }
     
  2. 1) For running code async, an ExecutorService can offer more performance
    2) One ExecutorService = one thread. You queue multiple actions after another on the same thread, so database actions wait on each other. It will not finish unless you close it.
    3) As far as I know, HikariCP calls are not asynchronous, but I could be mistaken.
     
  3. 1. afaik, bukkit runnables run in time with the server tick, whereas the threads stored in your executor wouldn’t.
    2. The threads in an executor will stay there until you shut them down, or the program shuts down.
    3. Not sure, I’ve never used hikari directly, rather just wrappers around it. I’d assume you should execute the queries async however.
     
  4. Choco

    Moderator

    That is not necessarily true. Fun fact: The BukkitScheduler runs on an ExecutorService internally.

    This depends entirely on the implementation you use. ExecutorService is an interface for a reason. There are dozens that each do different things in the Executors factory class, some of which do do what you're describing, others don't. @OP please read up on which service you're using and figure out which will be best for the task at hand.

    They are not. That's on you to handle.
     
  5. Thanks to all the anwers.

    About what Choco said, I'm using the FixedThreadPool and my friend said that it lets the threads in a waiting mode after the mysql update, and that's could lag the bukkit main thread.
     
  6. Choco

    Moderator

    The two threads are unrelated. That's the point of a thread pool. At most you're likely only going to need maybe 1 or 2 threads in your thread pool depending on how frequently you plan on executing an SQL query. (Assuming 2 threads) A fixed thread pool will keep those 2 threads sleeping until a task is queued at which point one of the two threads will be used. If both threads are being used and a third task is queued, it will remain in queue until one of the other threads has completed its task. It should not halt the main thread at all.

    I agree that a fixed thread pool is the best option in this case. Again, depending on how many queries you expect to be running at any given time, I advise 1 or 2 threads to be used.
     
    • Friendly Friendly x 1
  7. I'm really grateful for your answer. But my friend's point is that many threads on waiting mode could lag the server. Is it correct?
     
  8. No.