Created
January 24, 2020 04:33
-
-
Save Muqsit/88ef84765e55e32f446ea9762c428c29 to your computer and use it in GitHub Desktop.
test
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| <?php | |
| require "vendor/autoload.php"; | |
| use pocketmine\snooze\SleeperNotifier; | |
| use pocketmine\snooze\SleeperHandler; | |
| class MyThread extends \Thread{ | |
| private $notifier; | |
| public $result; | |
| public function __construct(SleeperNotifier $notifier){ | |
| $this->notifier = $notifier; | |
| } | |
| public function executeQuery() : void{ | |
| $this->synchronized(function() : void{ | |
| $this->notify(); | |
| }); | |
| } | |
| public function run() : void{ | |
| while(true){ | |
| $this->result = mt_rand() / mt_getrandmax(); | |
| $this->notifier->wakeupSleeper(); | |
| $this->synchronized(function() : void{ | |
| $this->wait(); | |
| }); | |
| } | |
| } | |
| public function setGarbage(){ | |
| } | |
| } | |
| $handler = new SleeperHandler(); | |
| $notifier = new SleeperNotifier(); | |
| $thread = new MyThread($notifier); | |
| $handler->addNotifier($notifier, function() use($thread) : void{ | |
| echo "Received result: " . $thread->result . PHP_EOL; | |
| }); | |
| $thread->start(); | |
| $i = 0; | |
| while(++$i <= 10){ | |
| $thread->executeQuery(); | |
| $handler->sleepUntilNotification(); | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment