php多线程代码

<?php
$thNum = 20; //20个进程
$total = 20000;//总数
$pageNum=100;//每个页面显示100条数据

$pageCount = ceil($total/$pageNum);//总页数 200
$thCount =ceil($pageCount/20); // 10个进程

for($i=1;$i<$thCount;$i++){
$start = ($i-1)*($thNum-1)+$i;//线程开始
$end = $i*$thNum;//线程结束
// echo $start.'==>'.$end;exit;
for($j=$start;$start<$end;$start++){
$th = new muThread();
$th->start();
$th->join();
}
}

/**
* 多线程类
*
* Class muThread
*/
class muThread extends Thread
{

public function __construct()
{
}

public function run()
{
echo 'time:' . date('Y-m-d H:i:s') . " ";
//下面就可以操作你要操作的数据
}
}

?>

上面这个是一个比较简单的多线程,上面没有任何对数据库的操作,只是一个非常简单的demo。使用者可以copy去运行,运行只能通过后台去运行。页面上运行会报错,说类没有找到。

原文地址:https://www.cnblogs.com/kobigood/p/5892777.html