Câu hỏi phỏng vấn PHP
Câu hỏi

Does PHP hav...

Câu trả lời

Yes, PHP does support multithreading and parallel execution, although it is not built-in to the core language. To achieve multithreading in PHP, you need to use the pthreads extension[1][2].

The pthreads extension provides classes and methods for creating, managing, and synchronizing threads in PHP. It allows you to create independent units of execution called threads that can run concurrently[1].

Here's a simple example of how to create and manage threads using pthreads[3]:

php Copy
class PThread extends Thread {
    public function run() {
        $sleep = mt_rand(1, 10);
        printf('Thread: %s has started, sleeping for %d'. "\n", $this->id, $sleep);
        sleep($sleep);
        printf('Thread: %s has finished'. "\n", $this->id);
    }
}

$poolArr = array();
foreach (range("0", "3") as $i) {
    $poolArr[] = new PThread($i);
}

foreach ($poolArr as $t) {
    $t->start();
}

foreach ($poolArr as $t) {
    $t->join();
}

This code creates four threads, each sleeping for a random duration between 1 and 10 seconds. The threads are started concurrently and then joined back together at the end.

While...

senior

senior

Gợi ý câu hỏi phỏng vấn

middle

What are some of the big changes PHP has gone through in the past few years?

middle

What is the difference between a PHP interpreter and a PHP handler?

middle

What is use of Null Coalesce Operator?

Bình luận

Chưa có bình luận nào

Chưa có bình luận nào