note 118518 deleted from threaded.from by cmb
| From: | cmb@php.net | Date: | Sat, 19 Dec 2015 12:51:16 +0000 |
| Subject: | note 118518 deleted from threaded.from by cmb | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-204490@lists.php.net to get a copy of this message | ||
Note Submitter: Anonymous
----
These code can't Run! Why?????????
class Account extends Thread{
private $balance;
public function __construct($balance){
$this->balance = $balance;
}
public function draw($cost){
if($this->balance >= $cost){
sleep(1);
$this->balance -= $cost;
echo Thread::getCurrentThreadId() . "Reduce ${cost} successfulï¼now the balance is
ï¼" . $this->balance . "<Br/>";
}
else
echo Thread::getCurrentThreadId() . "Reduce failï¼now the balance still isï¼" .
$this->balance . "<Br/>";
}
}
$account = new Account(200);
for($i=0;$i<3;$i++){
$ts[] = Threaded::from(function(){
$this->account->draw(80);
},function($account){
$this->account = $account;
},[$account]);
}
$worker = new Worker();
foreach($ts as $t){
$worker->stack($t);
}
$worker->start();