mirror of
https://github.com/DesignPatternsPHP/DesignPatternsPHP.git
synced 2025-07-31 12:10:10 +02:00
it was created the Creational namespace and append its patterns
This commit is contained in:
53
Creational/Pool/Processor.php
Normal file
53
Creational/Pool/Processor.php
Normal file
@@ -0,0 +1,53 @@
|
||||
<?php
|
||||
|
||||
namespace DesignPatterns\Creational\Pool;
|
||||
|
||||
class Processor
|
||||
{
|
||||
|
||||
private $pool;
|
||||
private $processing = 0;
|
||||
private $maxProcesses = 3;
|
||||
private $waitingQueue = [];
|
||||
|
||||
public function __construct(Pool $pool)
|
||||
{
|
||||
$this->pool = $pool;
|
||||
}
|
||||
|
||||
public function process($image)
|
||||
{
|
||||
if ($this->processing++ < $this->maxProcesses) {
|
||||
$this->createWorker($image);
|
||||
} else {
|
||||
$this->pushToWaitingQueue($worker);
|
||||
}
|
||||
}
|
||||
|
||||
private function createWorker($image)
|
||||
{
|
||||
$worker = $this->pool->get();
|
||||
$worker->run($image, array($this, 'processDone'));
|
||||
}
|
||||
|
||||
public function processDone($worker)
|
||||
{
|
||||
$this->processing--;
|
||||
$this->pool->dispose($worker);
|
||||
|
||||
if (count($this->waitingQueue) > 0) {
|
||||
$this->createWorker($this->popFromWaitingQueue());
|
||||
}
|
||||
}
|
||||
|
||||
private function pushToWaitingQueue($image)
|
||||
{
|
||||
$this->waitingQueue[] = $image;
|
||||
}
|
||||
|
||||
private function popFromWaitingQueue()
|
||||
{
|
||||
return array_pop($this->waitingQueue);
|
||||
}
|
||||
|
||||
}
|
Reference in New Issue
Block a user