首頁 > 後端開發 > php教程 > php 佇列類別 訊息佇列 thinkphp 佇列 php佇列處理高並

php 佇列類別 訊息佇列 thinkphp 佇列 php佇列處理高並

WBOY
發布: 2016-07-29 08:52:05
原創
3128 人瀏覽過

<code><span><span><?php</span><span>/** 
 * PHP Class for queue 
 *<span> @author</span> yangqijun@live.cn 
 *<span> @copyright</span> DataFrog Beijingbei  Ltd. 2011-07-25 
 */</span><span><span>class</span><span>Queue</span> {</span><span>public</span><span>$length</span>=<span>12</span>;          <span>//默认队列,相当于初始化队列</span><span>public</span><span>$queue</span>  = <span>array</span>();  <span>//  if String like this "22,23,24"    convert to array to do queue  </span><span>public</span><span>$delimiter</span>=<span>','</span>;  

    <span><span>function</span><span>__construct</span><span>(<span>$queue</span>=array<span>()</span>)</span>    {</span><span>$this</span>->queue=<span>$queue</span>;
    }  
    <span>/** 
     *<span> @desc</span> start queue 
     *<span> @param</span> String  $param  new queue element 
     */</span><span>public</span><span><span>function</span><span>run</span><span>(<span>$param</span>)</span>    {</span><span>if</span>(!is_array(<span>$this</span>->queue)){  
            <span>$this</span>->strToQue();<span>//将数组视为队列 </span>
        }  
        <span>$currentlength</span>=<span>$this</span>->countqueue();        <span>//Count  the  queue length</span><span>echo</span><span>$currentlength</span>;
        <span>echo</span><span>$this</span>->length.<span>'<br>'</span>;   
        <span>if</span>(<span>$currentlength</span><<span>$this</span>->length&&<span>$this</span>->length><span>0</span>) {  
            <span>$this</span>->queAdd(<span>$param</span>);  
        }<span>else</span><span>if</span>(<span>$this</span>->length==<span>0</span>)  <span>//如果为空队列,则将队列初始化为输入的队列</span>
        {  
            <span>$param</span>=<span>empty</span>(<span>$param</span>)?<span>0</span>:<span>$param</span>;  
            <span>$this</span>->queue[]=<span>$param</span>;  
        }  
        <span>else</span> {              
            <span>for</span> (<span>$i</span>=<span>0</span>;<span>$i</span><<span>$currentlength</span>-<span>$this</span>->length-<span>1</span>;<span>$i</span>++)<span>//队列比规定的队列多,要删掉队首的元素,才能入队</span>
            {  
                <span>$this</span>->queRemove();  
            }  
            <span>$this</span>->queAdd(<span>$param</span>);  
        }               
        <span>return</span><span>$this</span>->queue;               
    }  
    <span>/** 
     * String like this "22,23,24"  convert to array to do queue 
     *<span> @param</span> String $string 
     *<span> @param</span> String $delimiter 
     */</span><span>public</span><span><span>function</span><span>strToQue</span><span>()</span>{</span><span>if</span> (<span>empty</span>(<span>$this</span>->queue))  
        {  
            <span>$this</span>->queue=<span>array</span>();  
        }  
        <span>else</span>        {  
            <span>$this</span>->queue=explode(<span>$this</span>->delimiter,<span>$this</span>->queue);  
        }       
    }  
    <span>/** 
     * insert $node into queue 
     *<span> @param</span> string $node 
     */</span><span>private</span><span><span>function</span><span>queAdd</span><span>(<span>$node</span>)</span>{</span>        array_push(<span>$this</span>->queue,<span>$node</span>);  
        <span>$this</span>->countqueue();  
    }  
    <span>private</span><span><span>function</span><span>queRemove</span><span>()</span>{</span><span>$node</span> = array_shift(<span>$this</span>->queue);  
        <span>$this</span>->countqueue();  
        <span>return</span><span>$node</span>;  
    }  
    <span>private</span><span><span>function</span><span>countqueue</span><span>()</span>{</span><span>$currentlength</span>= count(<span>$this</span>->queue);  
        <span>return</span><span>$currentlength</span>;  
    }  
    <span><span>function</span><span>__destruct</span><span>()</span>    {</span><span>unset</span>(<span>$this</span>->queue);  
    }  
}   
<span>//example  </span><span>$str</span>=<span>'88|89|90|56|23|45|69|23|20|100'</span>; 
<span>$obj</span>=<span>new</span> Queue (<span>$str</span>); 
<span>$obj</span>->length=<span>8</span>;  <span>// 队列元素长度  </span><span>$obj</span>->delimiter=<span>'|'</span>;  <span>//如果队列是字符串,则元素直接的分隔符为| </span><span>$a</span>=<span>$obj</span>->run(<span>'91'</span>);   <span>//要添加到队列中的元素 </span><span>$a</span>=<span>$obj</span>->run(<span>'92'</span>);  
<span>$a</span>=<span>$obj</span>->run(<span>'93'</span>);  
<span>$a</span>=<span>$obj</span>->run(<span>'94'</span>);  
print_r(<span>$a</span>);
<span>?></span></span></span></code>
登入後複製
').addClass('pre-numbering').hide(); $(this).addClass('has-numbering').parent().append($numbering); for (i = 1; i ').text(i)); }; $numbering.fadeIn(1700); }); });

以上就介紹了php 隊列類,包括了php,隊列方面的內容,希望對PHP教程有興趣的朋友有幫助。

相關標籤:
來源:php.cn
本網站聲明
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn
熱門教學
更多>
最新下載
更多>
網站特效
網站源碼
網站素材
前端模板