This article describes the PHP multithreading concurrency implementation method. Share to everyone for your reference, specific as follows:
In Java, multithreading is a new thread of things, PHP relies on the Apache Linux Bottom has a multithreaded approach.
Here's how to simulate PHP concurrency if you can't control Apache servers
<?php
if (function_exists (' Date_default_timezone_set ')) {
date_default_timezone_set (' PRC ');
}
function A ()
{
$time = time ();
Sleep (3);
$fp = fopen (' result_a '. $time. Log ', ' W ');
Fputs ($fp, ' Set in '. Date (' H:i:s ', Time ()). (double) microtime (). "RN");
Fclose ($FP);
Function B ()
{
$time = time ();
Sleep (3);
$fp = fopen (' Result_b '. $time. Log ', ' W ');
Fputs ($fp, ' Set in '. Date (' H:i:s ', Time ()). (double) microtime (). "RN");
Fclose ($FP);
if (!isset ($_get[' act ')) $_get[' act ' = ' a ';
if ($_get[' act '] = = = ' A ')
{
A ();
}
else if ($_get[' act '] = = ' B ') b ();
? >
The above code to write a file locally.
If you access localhost/a.php the two browser tabs open as fast as possible, you find that two file creation time is 3 seconds
But if you visit localhost/a.php?act=b another visit to/a.php?act=a you find that two files are created almost the same time.
For Apache, the same URL means a thread (we or a process), but a different URL means that it can be concurrent.
If there is a download action inside PHP
function Runthread ()
{down
("http://localhost/test/a.php?act=a");
}
if ($_get[' act '] = = ' Run ')
{
echo ' start: ';
Runthread ();
echo ' End ';
}
Http://localhost/test/a.php?act=run
http://localhost/test/a.php?act=run&s=2
As long as the URL of the primary access is different, it is considered to be different, meaning concurrency. File creation time is not 3 seconds
A friend with a local Linux server can also use Linux to simulate concurrency.
<?php for
($i =0; $i <10; $i + +) {
echo $i;
Sleep (5);
}
? >
Save it as a test.php, then write a shell code
#!/bin/bash for
i in 1 2 3 4 5 6 7 8 9 Ten
do
php-q test.php &
Done
For more information on PHP related content readers can view the site topics: "PHP process and thread Operation skills Summary", "PHP array Operation techniques Encyclopedia", "PHP Sorting algorithm Summary", "PHP common traversal algorithm and skills summary", "PHP Data structure and algorithm tutorial", " PHP Programming algorithm Summary, "PHP Mathematical Calculation Skills Summary", "PHP Regular Expression Usage summary", "PHP operation and operator Usage Summary", "PHP string (String) Usage summary" and "PHP common database Operation skill Summary"
I hope this article will help you with the PHP program design.