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"); }}
@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.
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.
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.