Skip to content

Instantly share code, notes, and snippets.

@caiquecastro
Created November 7, 2016 13:48
Show Gist options
  • Save caiquecastro/ba86d39c363a13d708ed9edeb96d1a0d to your computer and use it in GitHub Desktop.
Save caiquecastro/ba86d39c363a13d708ed9edeb96d1a0d to your computer and use it in GitHub Desktop.
PHP tem thread
<?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