Last active
January 19, 2021 15:39
-
-
Save rickynguyen4590/cd0cea12ab0f7fb0aba08df040702b55 to your computer and use it in GitHub Desktop.
Code mẫu lập trình multi-thread trong php
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
class AsyncOperation extends Thread { | |
public function __construct($arg) { | |
$this->arg = $arg; | |
} | |
public function run() { | |
if ($this->arg) { | |
$sleep = mt_rand(1, 10); | |
printf('%s: %s -start -sleeps %d' . "\n", date("g:i:sa"), $this->arg, $sleep); | |
sleep($sleep); | |
printf('%s: %s -finish' . "\n", date("g:i:sa"), $this->arg); | |
} | |
} | |
} | |
// Create a array | |
$stack = array(); | |
//Initiate Multiple Thread | |
foreach ( range("A", "D") as $i ) { | |
$stack[] = new AsyncOperation($i); | |
} | |
// Start The Threads | |
foreach ( $stack as $t ) { | |
$t->start(); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment