Guest User

Untitled

a guest
Nov 14th, 2018
103
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 3.20 KB | None | 0 0
  1. <?php  
  2.     class SleepThread extends Thread
  3.     {
  4.         public $hit = false;
  5.  
  6.         public function __construct($nap, $n)
  7.         {
  8.             $this->nap = $nap;
  9.             $this->thread_no = $n;
  10.         }
  11.  
  12.         public function run()
  13.         {
  14.             while (1)
  15.             {
  16.                 $r = mt_rand(0, 9);
  17.                 echo $r . " \tdrawn in thread " . $this->thread_no . "\n";
  18.                 if ($r == 9)
  19.                 {
  20.                     $this->hit = true;
  21.                 }
  22.                 if ($this->hit)
  23.                 {
  24.                     $this->synchronized
  25.                     (
  26.                         function ()
  27.                         {
  28.                             $job = new stdClass;
  29.                             $job->message = "Test\n";
  30.                             echo "*CHILD:\tHit... \n";
  31.                             $this->result = 'Found in thread ' .  $this->thread_no . ' at ' . time() . ".  You're a thread master...";
  32.                             echo "*CHILD:\tWaiting for parent thread ... \n";
  33.                             $time = microtime();
  34.                             $this->wait();
  35.                             echo "*CHILD:\tDone waiting after " . (microtime() - $time) . " seconds\n";
  36.                             $this->hit = false;
  37.                             echo "*CHILD:\tChild " . $this->thread_no . " taking a long nap...\n";
  38.                             sleep(3 * $this->nap);
  39.                             echo "*CHILD:\tChild thread continuing. Thread number is:  " . $this->thread_no . "...\n";
  40.                         }
  41.                     );
  42.                 }
  43.                 sleep($this->nap);
  44.             }
  45.         }
  46.     }
  47.    
  48.     $thread[0] = new SleepThread(5, "0");
  49.     $thread[1] = new SleepThread(5, "1");
  50.     $thread[2] = new SleepThread(5, "2");
  51.     foreach ($thread as $job)
  52.     {
  53.         $job->start();
  54.     }
  55.    
  56.     while (1)
  57.     {
  58.         sleep(1);
  59.         foreach ($thread as $job)
  60.         {
  61.             if ($job->hit)
  62.             {
  63.                 echo ("PARENT:\tDetected a hit in thread " . $job->thread_no . "\n");
  64.                 $job->synchronized
  65.                 (
  66.                     function ($job)
  67.                     {
  68.                         echo "PARENT:\tNotifying child thread that we are ready for its result.\n";
  69.                         $job->notify();
  70.                         echo "PARENT:\t" . $job->result . "  <----That's the result.\n";
  71.                         // echo "PARENT:  " . $job->obj->message . "^^^Getting object created in child thread oesn't work for some rasons^^^.\n";
  72.                     },
  73.                     $job
  74.                 );
  75.             }
  76.         }
  77.     }
  78. /*
  79. 3       drawn in thread 0
  80. 3       drawn in thread 1
  81. 0       drawn in thread 2
  82. 0       drawn in thread 0
  83. 0       drawn in thread 1
  84. 9       drawn in thread 2
  85. *CHILD: Hit...
  86. *CHILD: Waiting for parent thread ...
  87. PARENT: Detected a hit in thread 2
  88. PARENT: Notifying child thread that we are ready for its result.
  89. PARENT: Found in thread 2 at 1542168674.  You're a thread master...  <----That's the result.
  90. *CHILD: Done waiting after 0.00082700000000002 seconds
  91. *CHILD: Child 2 taking a long nap...
  92. 8       drawn in thread 0
  93. 0       drawn in thread 1
  94. 5       drawn in thread 0
  95. 9       drawn in thread 1
  96. *CHILD: Hit...
  97. *CHILD: Waiting for parent thread ...
  98. 4       drawn in thread 0
  99. *CHILD: Child thread continuing. Thread number is:  2...
  100. PARENT: Detected a hit in thread 1
  101. PARENT: Notifying child thread that we are ready for its result.
  102. PARENT: Found in thread 1 at 1542168684.  You're a thread master...  <----That's the result.
  103. *CHILD: Done waiting after 0.004718 seconds
  104. *CHILD: Child 1 taking a long nap...
  105. 6       drawn in thread 0
  106. 7       drawn in thread 2
  107. 2       drawn in thread 0
  108. 5       drawn in thread 2
  109. 3       drawn in thread 0
  110. 2       drawn in thread 2
  111. *CHILD: Child thread continuing. Thread number is:  1...
  112. 1       drawn in thread 0
  113. 5       drawn in thread 2
  114. 5       drawn in thread 1
  115.  
  116. */
  117. ?>
Add Comment
Please, Sign In to add comment