HosipLan

Untitled

Apr 6th, 2011
293
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 0.72 KB | None | 0 0
  1. class Counter
  2. {
  3.  
  4.     public $maxCount = 50;
  5.  
  6.     private $count = 0;
  7.  
  8.  
  9.     public function increase()
  10.     {
  11.         if ($this->count === $this->maxCount) {
  12.             throw new \OutOfBoundsException;
  13.         }
  14.  
  15.         $this->count++;
  16.     }
  17.  
  18.    
  19.     public function getCount()
  20.     {
  21.         return $this->count
  22.     }
  23.  
  24. }
  25.  
  26.  
  27.  
  28.  
  29. class Increasor implements Runnable
  30. {
  31.  
  32.     /** @var Counter */
  33.     private $counter;
  34.  
  35.  
  36.     public function __construct(Counter $counter)
  37.     {
  38.         $this->counter = $counter;
  39.     }
  40.  
  41.  
  42.     public function run()
  43.     {
  44.         while (TRUE) {
  45.             try {
  46.                 $this->counter->increase();
  47.             } catch (\utOfBoundsException $e) {
  48.                 break;
  49.             }
  50.         }
  51.     }
  52. }
  53.  
  54.  
  55. $counter = new Couter();
  56.  
  57. for ($i=0; $i < 3 ;$i++) {
  58.     new Thread(new Increasor($counter)).start();
  59. }
Advertisement
Add Comment
Please, Sign In to add comment