@@ -5,48 +5,48 @@ |
||
| 5 | 5 | // Method to be executed in a thread |
| 6 | 6 | function Foo($t) |
| 7 | 7 | { |
| 8 | - echo "Starting thread #$t" . PHP_EOL;; |
|
| 8 | + echo "Starting thread #$t" . PHP_EOL;; |
|
| 9 | 9 | sleep(1 * rand(1, 5)); |
| 10 | - for ($i = 0; $i < 10; $i++) |
|
| 11 | - { |
|
| 12 | - echo "Hello from thread #$t, i=$i" . PHP_EOL; |
|
| 13 | - sleep(1); |
|
| 14 | - } |
|
| 10 | + for ($i = 0; $i < 10; $i++) |
|
| 11 | + { |
|
| 12 | + echo "Hello from thread #$t, i=$i" . PHP_EOL; |
|
| 13 | + sleep(1); |
|
| 14 | + } |
|
| 15 | 15 | echo "Ending thread #$t" . PHP_EOL; |
| 16 | 16 | |
| 17 | - return uniqid("Thread_{$t}_"); |
|
| 17 | + return uniqid("Thread_{$t}_"); |
|
| 18 | 18 | } |
| 19 | 19 | |
| 20 | 20 | |
| 21 | 21 | try |
| 22 | 22 | { |
| 23 | - // Create a instance of the ThreadPool |
|
| 24 | - $threadPool = new \ByJG\PHPThread\ThreadPool(); |
|
| 23 | + // Create a instance of the ThreadPool |
|
| 24 | + $threadPool = new \ByJG\PHPThread\ThreadPool(); |
|
| 25 | 25 | |
| 26 | - // Create the threads |
|
| 26 | + // Create the threads |
|
| 27 | 27 | for ($i = 0; $i < 10; $i++) |
| 28 | 28 | { |
| 29 | - // Queue a worker pointing to "Foo" function and pass the required parameters |
|
| 29 | + // Queue a worker pointing to "Foo" function and pass the required parameters |
|
| 30 | 30 | $threadPool->queueWorker('Foo', [ $i ]); |
| 31 | 31 | } |
| 32 | 32 | |
| 33 | - // Starts all the threads in the queue |
|
| 34 | - $threadPool->startWorkers(); |
|
| 33 | + // Starts all the threads in the queue |
|
| 34 | + $threadPool->startWorkers(); |
|
| 35 | 35 | |
| 36 | - // Wait until there is no more active workers |
|
| 37 | - while($threadPool->activeWorkers() > 0) |
|
| 38 | - { |
|
| 39 | - echo "Active Workers : " . $threadPool->activeWorkers() . "\n"; |
|
| 40 | - sleep(1); |
|
| 41 | - } |
|
| 36 | + // Wait until there is no more active workers |
|
| 37 | + while($threadPool->activeWorkers() > 0) |
|
| 38 | + { |
|
| 39 | + echo "Active Workers : " . $threadPool->activeWorkers() . "\n"; |
|
| 40 | + sleep(1); |
|
| 41 | + } |
|
| 42 | 42 | |
| 43 | - // Get the return value from the thread. |
|
| 44 | - foreach ($threadPool->getThreads() as $thid) |
|
| 45 | - { |
|
| 46 | - echo 'Result: ' . $threadPool->getThreadResult($thid) . "\n"; |
|
| 47 | - } |
|
| 43 | + // Get the return value from the thread. |
|
| 44 | + foreach ($threadPool->getThreads() as $thid) |
|
| 45 | + { |
|
| 46 | + echo 'Result: ' . $threadPool->getThreadResult($thid) . "\n"; |
|
| 47 | + } |
|
| 48 | 48 | |
| 49 | - echo "\n\nEnded!\n"; |
|
| 49 | + echo "\n\nEnded!\n"; |
|
| 50 | 50 | |
| 51 | 51 | } |
| 52 | 52 | catch (Exception $e) |