Guest User

Untitled

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