Advertisement
  1. This site uses cookies. By continuing to use this site, you are agreeing to our use of cookies. Learn More.

sleep a pocketmine\Thread

Comments in 'Plugin Development' started by Kripto, Mar 19, 2015.

  1. Kripto
    Offline

    Kripto New Member

    Joined:
    Mar 2, 2014
    Posts:
    15
    Minecraft User:
    Kripto96
    I created a new Thread using the pocketmine\Thread class. How can I use the sleep() (or similar) function only in this thread? (If I use sleep() now, also the Main thread sleep).
    This is my current code:
    PHP:
    use pocketmine\Server;
    use 
    pocketmine\Thread;
    class 
    Task extends Thread {

        public function 
    run() {
            
    sleep(10);
            
    Server::getInstance()->broadcastMessage("Ready");
        }

    }
  2. aliuly
    Offline

    aliuly Notable Member Plugin Developer

    Joined:
    Feb 8, 2014
    Posts:
    1,086
    Plugins:
    17
    You need an AsyncTask and schedule it accordingly.
  3. Falk
    Offline

    Falk Staff Member Global Moderator

    Joined:
    Sep 2, 2013
    Posts:
    1,706
    Plugins:
    22
    Minecraft User:
    Falkirknh
    @aliuly is incorrect. Async tasks don't cover all use cases. Since a thread is running in a separate thread, sleep won't disturb the main thread. If it is, you are doing something wrong, make sure you are spawning the thread correctly (don't call run(), call start() :)). There is also Threaded::wait($timeout) but for what your doing sleep will accomplish the same thing. Also, you will get a segfault if that runs on another thread as the memory it is trying to access belongs to the main thread.
  4. PEMapModder
    Offline

    PEMapModder Notable Member Plugin Developer

    Joined:
    Oct 9, 2013
    Posts:
    7,294
    Plugins:
    11
    Minecraft User:
    PEMapModder
    Do not call PocketMine non-static functions from other threads.
    Also do not call functions that involve static properties in other threads. It may cause problems.
  5. mamayadesu
    Offline

    mamayadesu Active Member Plugin Developer

    Joined:
    Sep 19, 2013
    Posts:
    146
    Plugins:
    4
    Minecraft User:
    mamayadesu
    How right to use own Threads? Can you give example, please?
  6. PEMapModder
    Offline

    PEMapModder Notable Member Plugin Developer

    Joined:
    Oct 9, 2013
    Posts:
    7,294
    Plugins:
    11
    Minecraft User:
    PEMapModder
    I forgot to mention, if you have plugins that heavily depend on AsyncTask, and you fire several suspended AsyncTask, the async workers will be occupied and the plugins that heavily depend on AsyncTask will not work because there will be no new async workers to accept them.
    So, if you plan to use sleep(), you'd better start a thread rather than firing an AsyncTask.

    An AsyncTask is run in another thread, but starting an AsyncTask doesn't start a thread.
    Legoboy0215 and PocketKiller like this.

Share This Page

Advertisement