라이브러리

[PHP] Threaded::notify - 동기화




PHP에서 Threaded::notify

PHP는 기본적으로 싱글 스레드 언어로, 하나의 스레드만 실행할 수 있습니다. 하지만, PHP 7.4부터는 Threaded 클래스를 통해 멀티 스레딩을 지원합니다. Threaded::notify는 스레드 간의 통신을 위한 메소드입니다.

Threaded::notify 사용법

Threaded::notify는 스레드 간의 통신을 위해 사용됩니다. notify 메소드는 스레드에 메시지를 보낼 수 있습니다. 받는 스레드는 wait 메소드를 사용하여 notify 메시지를 기다릴 수 있습니다.

예제 1: Threaded::notify 사용하기


#hostingforum.kr
php

class Thread extends Threaded {

    private $name;



    public function __construct($name) {

        $this->name = $name;

    }



    public function run() {

        echo $this->name . " 스레드가 실행 중입니다.
";

        sleep(2);

        echo $this->name . " 스레드가 종료되었습니다.
";

    }

}



class MainThread extends Threaded {

    private $thread;



    public function __construct() {

        $this->thread = new Thread("Child Thread");

        $this->thread->start();

        $this->wait();

        echo "Main Thread가 종료되었습니다.
";

    }



    public function run() {

        echo "Main Thread가 실행 중입니다.
";

        sleep(1);

        $this->thread->notify();

    }

}



$mainThread = new MainThread();



예제 2: Threaded::notify 사용하기 (wait와 notify)


#hostingforum.kr
php

class Thread extends Threaded {

    private $name;



    public function __construct($name) {

        $this->name = $name;

    }



    public function run() {

        echo $this->name . " 스레드가 실행 중입니다.
";

        sleep(2);

        echo $this->name . " 스레드가 종료되었습니다.
";

    }

}



class MainThread extends Threaded {

    private $thread;



    public function __construct() {

        $this->thread = new Thread("Child Thread");

        $this->thread->start();

        $this->wait();

        echo "Main Thread가 종료되었습니다.
";

    }



    public function run() {

        echo "Main Thread가 실행 중입니다.
";

        sleep(1);

        $this->thread->notify();

        $this->wait(); // notify를 받은 후 wait를 사용하여 스레드가 종료되기를 기다립니다.

        echo "Main Thread가 종료되었습니다.
";

    }

}



$mainThread = new MainThread();



예제 3: Threaded::notify 사용하기 (notifyAll)


#hostingforum.kr
php

class Thread extends Threaded {

    private $name;



    public function __construct($name) {

        $this->name = $name;

    }



    public function run() {

        echo $this->name . " 스레드가 실행 중입니다.
";

        sleep(2);

        echo $this->name . " 스레드가 종료되었습니다.
";

    }

}



class MainThread extends Threaded {

    private $thread1;

    private $thread2;



    public function __construct() {

        $this->thread1 = new Thread("Child Thread 1");

        $this->thread2 = new Thread("Child Thread 2");

        $this->thread1->start();

        $this->thread2->start();

        $this->wait();

        echo "Main Thread가 종료되었습니다.
";

    }



    public function run() {

        echo "Main Thread가 실행 중입니다.
";

        sleep(1);

        $this->thread1->notify();

        $this->thread2->notify();

    }

}



$mainThread = new MainThread();



위의 예제에서, Threaded::notify는 스레드 간의 통신을 위해 사용됩니다. notify 메소드는 스레드에 메시지를 보낼 수 있습니다. 받는 스레드는 wait 메소드를 사용하여 notify 메시지를 기다릴 수 있습니다. notifyAll 메소드는 모든 스레드에 notify 메시지를 보낼 수 있습니다.
  • profile_image
    나우호스팅 @pcs8404 

    호스팅포럼 화이팅!

    댓글목록

    등록된 댓글이 없습니다.

  • 전체 8,985건 / 85 페이지

검색

게시물 검색