Multithreading

Discussion in 'Plugin Development' started by edocsyl, Jun 16, 2012.

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

    edocsyl

    Hi

    I need to retrieve data from an API. Sometimes this takes up to 10 sek.
    Now i wanna inform the player, that this could take some time.
    Like so.

    [​IMG]

    Any idea how i can make this.

    Greez Edocsyl
     
  2. Offline

    LexLaiden

    Create a class that does your background work.
    Code:
    class someClass implements Runnable{
        private yourplugin plugin;
        private Player player;
        private somedatatype somedata=null;
     
        public yourplugin(yourplugin p, String playername, somedatatype somedata){
            plugin=p;
            // Don't pass an instance of player just pass the name and
            // get an instance inside this runnable thread
            player=plugin.getServer().getPlayerExact(playername);
            // if somedata is type pointer of string, array or another class
            // then you will have to create a new pointer to this data
            // if it is simple data types like int or long or... just use it
            this.somedata=somedata;
        }
     
        //Entry point
        public void run(){
     
            //put your code that will take awhile here
            // Then when done just
            If(player!=null){
                player.sendMessage("some message. Done!");
            } 
        }
     
    }
    As long as you don't need to call any non-ThreadSafe API calls this is waht you do
    Put this somewhare in you code that test if you need to do this background stuff
    Code:
    if(something==somthing){
     
        Runnable yp=new someClass(plugin, player.getName(), somedata);
        Thread thread = new Thread(yp);
        // Start the thread
        thread.start();
        // this runs on a separet thread, so it is indedentent
        // of the main thread and wont cause a hold up or lag
     
        player.sendMessage("This may take some time");
    }
    If you do need some non-ThreadSafe API calls then that is another discussion of how to que them to the main thread.
    The above code will create a separet Thread to run your background stuff and then ends when done.
     
  3. Offline

    edocsyl

    Thx very much! Awesom!
     
  4. you giving him n non thread safe example, plugin.getServer().getPlayerExact(playername); is not thread safe
     
  5. Offline

    LexLaiden

    Yes it is! I use it in this way to an extream amount and never had a problem yet.
    "Some" of the Player Memeber funtions are not thread safe, such as player.getNearbyEntities(x,y,z);
    but plugin.getServer().getPlayerExact(playername); in the constructor is safe.
     
  6. but player.sendMessage("some message. Done!"); is not threadsafe, and thats also is inside the run block
    EDIT: no problems dont mean its thread safe, thread bugs only occur someetimes on havy load, and not on normal load
     
  7. Offline

    Korvacs

    How isnt that threadsafe?
     
  8. the implementation of craftbukkit have an field called "entity" inside CraftPlayer, thats is not thread safe implemented, Player.sendMessage() depends on that not thread safe field, so it may throw an NPE when you try to acces that methode
     
  9. Offline

    Korvacs

    I dont really think you understand how threadsafe code works... I can create a class which has loads of collections which are accessed by a separate thread, but i can still call a method within that class without worrying about threadsafety so long as it doesnt interact with any of those collections.

    And infact your incorrect:

    Code:
        public void sendRawMessage(String message) {
            if (getHandle().netServerHandler == null) return;
            getHandle().netServerHandler.sendPacket(new Packet3Chat(message));
        }
        public void sendMessage(String message) {
            if (!conversationTracker.isConversingModaly()) {
                this.sendRawMessage(message);
            }
        }
    Thats all it does.
     
Thread Status:
Not open for further replies.

Share This Page