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]:

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

junior

Give me some real life examples when you had to use __destruct in your classes

junior

What are the main differences between const vs define

middle

Explain the difference between exec() vs system() vs passthru() ?

Bình luận

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

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