根据响应时间动态调整执行线程数的代码

直接上代码

<?php
interface Sender {
	public function send($message);
}
interface MessageQueue {
	public function pop();
}
class SendThread extends Thread {
	private $sender;
	private $messageQueue;
	protected $times;
	private $runtime;
	private $isStop;
	public function __construct(Sender $sender, MessageQueue $messageQueue, $times) {
		$this->sender = $sender;
		$this->messageQueue = $messageQueue;
		$this->times = $times;
		$this->runtime = 0;
		$this->isStop = false;
	}
	public function run() {
		while (!$this->isStop) {
			$runtime = 0;
			for ($i = 0; $i < $this->times; ++$i) {
				if (!$this->isStop) {
					$message = $this->messageQueue->pop();
					$start = microtime(true);
					$this->sender->send($message);
					$end = microtime(true);
					$runtime += ($end - $start);
				}
			}
			$this->runtime = $runtime;
		}
	}
	public function stop() {
		$this->isStop = true;
	}
	public function getRuntime() {
		return $this->runtime;
	}
}

abstract class ManageThread extends Thread {
	private $size;
	private $max;
	private $min;
	private $sleepTime;
	protected $times;
	
	private $avgRuntime;
	
	private $isStop;

	public function __construct($size, $max, $min, $sleepTime, $times) {
		$this->size = $size;
		$this->max = $max;
		$this->min = $min;
		$this->isStop = false;
		$this->sleepTime = $sleepTime;
		$this->times = $times;
		$this->avgRuntime = 0;
	}
	
	public function run() {
		$threadArray = array();
		for ($i = 0; $i < $this->size; ++$i) {
			$thread = $this->getNewSendThread();
			$threadArray[$i] = $thread;
			$thread->start();
		}
		while (!$this->isStop) {
			sleep($this->sleepTime);
			$sum = 0;
			for ($i = 0; $i < $this->size; ++$i) {
				$thread = $threadArray[$i];
				$sum += $thread->getRuntime();
			}
			$this->avgRuntime = $sum / $this->size;
			if ($this->avgRuntime > $this->max && $this->size > 1)
			{
				$threadArray[$this->size - 1]->stop();
				$threadArray[$this->size - 1] = null;
				--$this->size;
			}
			if ($this->avgRuntime < $this->min)
			{
				$threadArray[$this->size] = $this->getNewSendThread();
				++$this->size;
			}
		}
		for ($i = 0; $i < $this->size; ++$i) {
			$thread = $threadArray[$i];
			$thread->stop();
		}
		
	}
	public function stop() {
		$this->isStop = true;
	}
	abstract protected function getNewSendThread();
	public function getAvgRuntime() {
		return $this->avgRuntime;
	}
	public function getSize() {
		return $this->size;
	}
}

class MessegeSender implements Sender {
	public function send($message) {
		sleep(1); //响应时间
	}
}
class ICQMessageQueue implements MessageQueue {
	public function pop() {
		return "message";
	}
}
class MyManageThread extends ManageThread {
	protected function getNewSendThread() {
		return new SendThread(new MessegeSender(), new ICQMessageQueue(), $this->times);
	}
}

$manageThread = new MyManageThread(10, 10, 5, 20, 10);
$manageThread->start();
for ($i = 0; $i < 20; ++$i) {
	echo $manageThread->getAvgRuntime(), ' ', $manageThread->getSize(), "\n";
	sleep(10);
}
$manageThread->stop();
$manageThread->join();


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值