4

我目前想学习和实现pthreads-github。我已经编译php7了必要的组件并验证了它thread-safety已启用。我当前的示例代码有 4 个属于父类的子类Canine。我应该如何Thread相应地调用该类以同时bark()从下面显示的所有类中执行?记住当班级从 4 个增加到 100 个时扩展的能力。池?工人?

 class Database {
    private $_host;
    private $_user;
    private $_pass;
    private $_dbname;
    private $_dsn;
    private $_options;
    public static $pdo;
    private $error;

    private $stmt;

    public function __construct() {
        $this->_host   = 'xxxxx';
        $this->_user   = 'xxxxx';
        $this->_pass   = 'xxxxx';
        $this->_dbname = 'xxxxx';
        $this->_dsn    = 'mysql:host=' . $this->_host . ';dbname=' . $this->_dbname;
        // Set options
        $this->_options = array(
            PDO::MYSQL_ATTR_LOCAL_INFILE => true,
            PDO::ATTR_PERSISTENT         => true,
            PDO::ATTR_ERRMODE            => PDO::ERRMODE_EXCEPTION,
            PDO::ATTR_TIMEOUT            => 1,
        );
    }

    public function __sleep() {
        return array('_dsn', '_user', '_pass');
    }

    public function __wakeup() {
        $this->connect();
    }

    public function connect() {
        try {
            return self::$pdo = new PDO($this->_dsn, $this->_user, $this->_pass, $this->_options);
        } catch (PDOException $e) {
            $this->error = $e->getMessage();
        }
    }
}

class DataAccessObject {
    protected $database;

    public function __construct() {
        //Create Instance of DB
        $this->database = (new Database())->connect();
    }

    public function insertQuery($query) {
        try {
            $select = $this->database->prepare($query);
            $select->execute();
            $results = $select->fetchAll(PDO::FETCH_ASSOC);
        } catch (Exception $e) {
            echo "Failed: " . $e->getMessage();
        }
        return true;
    }
}

class Helper extends DataAccessObject {
//code elided
}

abstract class Canine extends Helper {
    abstract public function bark();
}

//Child Classes

class GoldenRitriever extends Canine {
    public function bark() { echo 'arf-arf!<br>'; }
}

class Pitbull extends Canine {
    public function bark() { echo 'ruff-ruff!<br>'; }
}

class GermanShepherd extends Canine {
    public function bark() { echo 'wao-wao!<br>'; }
}

class LabradorRetriever extends Canine {
    public function bark() { echo 'woof-woof!<br>'; }
}

Creating instances and invoking `bark()`

$golden_ritriever = new GoldenRitriever();
$golden_ritriever->bark();

$pitbull = new Pitbull();
$pitbull->bark();

$german_shepherd = new GermanShepherd();
$german_shepherd->bark();

$labrador_retriever = new LabradorRetriever();
$labrador_retriever->bark();
4

1 回答 1

4

我想您的问题在于解决方案的体系结构,即您将不得不在某个地方扩展 Thread 类并且不确定在哪里。

假设类结构(Helper,DataAccessObject)是最终的,并且使用 Thread 扩展 DataAcessObject 没有意义,我将按照以下方式进行。

class ThreadedCanine extends Thread {
    private $canine;
    public function __construct(Canine $canine) {
        $this->canine = $canine;
    }

    public function run() {
        $this->canine->bark();
    }
}

$threads = [
    new ThreadedCanine(new GoldenRitriever()),
    new ThreadedCanine(new Pitbull()),
    new ThreadedCanine(new GermanShepherd()),
    new ThreadedCanine(new LabradorRetriever()),
];

foreach($threads as $thread) {
    $thread->start();
}

foreach($threads as $thread) {
    $thread->join();
}

我认为这是一个好方法,因为它不会强制 Canine 成为线程,因为它可能不需要一直是线程。

编辑

我忘了在原始答案中提到,如果您想扩展到 100 个线程,是的,您应该使用PoolWorker类来管理它(文档甚至建议使用 Worker 而不是 Thread),否则启动 100同时线程不会很好(如果你有多个客户端会变得更糟):P

一旦您更好地掌握了使用线程,您就可以转到这些场景。

希望能帮助到你 :)

于 2016-09-07T15:56:06.667 回答