Created
November 7, 2016 13:48
-
-
Save caiquecastro/ba86d39c363a13d708ed9edeb96d1a0d to your computer and use it in GitHub Desktop.
PHP tem thread
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 | |
class SimpleWorkerThread extends Thread | |
{ | |
private $workerId = 0; | |
public function __construct($id) | |
{ | |
$this->workerId = $id; | |
} | |
public function run() | |
{ | |
echo "Worker " . $this->workerId . " começou a executar.\n"; | |
sleep(rand(0,4)); | |
echo "Worker " . $this->workerId . " parou de executar.\n"; | |
} | |
} | |
$workerPool = []; | |
foreach (range(0, 10) as $id) { | |
$workerPool[$id] = new SimpleWorkerThread($id); | |
$workerPool[$id]->start(); | |
} | |
foreach (range(0, 10) as $id) { | |
$workerPool[$id]->join(); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment