PHP Pthreads - 线程不并行运行

当我运行下面的代码时,单个线程似乎按顺序执行,一次执行。我期待他们能够平行运行。我的代码有什么问题,或者我应该开始看看我的环境?我在Windows机器上运行Apache,PHP 7。PHP Pthreads - 线程不并行运行

<?php 

class Searcher extends Worker

{

public $data = [];

public function addData($data)

{

$this->data[] = $data;

}

}

class MyThread extends Threaded

{

public function __construct($param)

{

$this->param = $param;

}

public function run()

{

sleep(3);

$this->worker->addData(

$this->param." - TIME: ".microtime(true)

);

}

}

$worker = new Searcher();

$params = ['thread1', 'thread2', 'thread3', 'thread4', 'thread5', 'thread6', 'thread7'];

foreach ($params as &$param) {

$worker->stack(new MyThread($param));

}

$worker->start();

$worker->join();

foreach ($worker->data as $data) {

echo $data."<br/>";

}

?>

代码的输出就永远成为这样的事情:

thread1 - TIME: 1512249412.3757 

thread2 - TIME: 1512249415.7776

thread3 - TIME: 1512249419.1792

thread4 - TIME: 1512249422.5813

thread5 - TIME: 1512249425.9825

thread6 - TIME: 1512249429.384

thread7 - TIME: 1512249432.7859

回答:

OK,我得到了现在。

工作人员不会并行运行所有堆积的线程,他们在一次执行一个线程。

要同时运行所有作业,必须使用Pool。

以上是 PHP Pthreads - 线程不并行运行 的全部内容, 来源链接: utcz.com/qa/258504.html

回到顶部