Execute Runnable on Plugin's Main Thread?

Discussion in 'BungeeCord Plugin Development' started by ReadySetPawn, Dec 19, 2017.

  1. Here's my code:

    Code (Java):
    getProxy().getScheduler().runAsync(this, new Runnable() {
                @Override
                public void run() {
                    getProxy().broadcast(Thread.currentThread().getName()); //This line prints out the name of the plugin's main thread
                    MysqlDataSource mysqlDataSource = new MysqlDataSource();
                    mysqlDataSource.setUser("");
                    mysqlDataSource.setPassword("");
                    mysqlDataSource.setServerName("");
                    mysqlDataSource.setDatabaseName("");

                    try {
                        Connection connection = mysqlDataSource.getConnection();
                        Statement statement = connection.createStatement();
                        ResultSet resultSet = statement.executeQuery("SELECT * FROM players;");
                        while (resultSet.next()) {
                            getProxy().broadcast(resultSet.getString(1));
                        }
                    } catch (SQLException e) {
                        e.printStackTrace();
                    }

                }
            });
    I want that code to run async, but I also want the option to execute certain code on the main plugin thread. Does BungeeCord have a
    Code (Java):
    .runLater(...);
    method? Also, I don't know why my code isn't even running async in the first place.

    Also, sorry about the ugly code. It's just for testing purposes until I can figure out how Bungee's concurrency works.
     
  2. I'm not sure about that but, async != multi threading an asynchronous runnable is not necessarily run on another thread. (It's the case in C# i'm not sure if it's the case in Java) that's maybe why your
    Code (Text):
    Thread.currentThread().getName()
    show the name of your main thread.

    and according to BungeeCord javadoc, there is no way to execute sync runnable.
     
    • Informative Informative x 1
  3. Thanks for the reply! Guess I'll just have to create and handle the threads on my own then, huh? I was really hoping there would be something in the API.