Returning from async thread without making server wait?

Discussion in 'Plugin Development' started by syntaxJedi, Dec 16, 2017.

Thread Status:
Not open for further replies.
  1. Offline

    syntaxJedi

    I'm having some trouble getting data from an async thread without returning null. This is the sort of idea i'm going for, obviously relList will always return null as the function continues to the end as soon as BukkitRunnable is encountered.

    Code:
    public static Map<Integer, Map<Integer, String>> getList(){
            Map<Integer, Map<Integer, String>> relList = new HashMap<Integer, Map<Integer, String>>();
            Map<Integer, String> relInfo = new HashMap<Integer, String>();
            BukkitRunnable l = new BukkitRunnable(){
                @Override
                public void run(){
                    Statement statement = null;
                    ResultSet result = null;
                    try{
                        statement = connection.createStatement();
                        result = statement.executeQuery("SELECT `key`, `name`, `description` FROM test.religions");
                        while(result.next()){
                            relInfo.put(1, result.getString(2));
                            relInfo.put(2, result.getString(3));
                            relList.put(result.getInt(1), relInfo);
                        }
                    }catch(SQLException e){
                        e.printStackTrace();
                    }
                }
            };
            l.runTaskAsynchronously(plugin);
            return relList;
    I need to call this method from another thread so I can manipulate the data. Is there a way I can return relList without making the server wait?
     
  2. Online

    timtower Administrator Administrator Moderator

    @syntaxJedi You need to call a method at the end of the runnable.
    Should use a runTask though to mame it sync
     
  3. Offline

    syntaxJedi

    To be sure I'm correct, you're telling me I should call getList() and then return to the original class with another method call to the manipulation underneath it? So something like:
    Code:
    onCommand(){
        if(args.length == 0){
            getList();
        }
    }
    
    returnList(){
        //data manipulation here
    }
     
Thread Status:
Not open for further replies.

Share This Page