Home >Backend Development >PHP Tutorial >Use of php thread extension

Use of php thread extension

WBOY
WBOYOriginal
2016-07-29 08:58:591857browse

Please refer to thread installation for the installation tutorial

Just upload a simple code

<code><span><?php</span><span><span>class</span><span>test</span><span>extends</span><span>Thread</span> {</span><span>public</span><span><span>function</span><span>__construct</span><span>(<span>$fun</span>)</span>
 {</span><span>$this</span>->fun=<span>$fun</span>;
 }

  <span>public</span><span><span>function</span><span>run</span><span>()</span> {</span><span>$param</span>=<span>$this</span>->fun;
    <span>$this</span>-><span>$param</span>();
  }

  <span>private</span><span><span>function</span><span>test1</span><span>()</span>
  {</span><span>echo</span><span>1</span>;
    <span>echo</span><span>"<br>"</span>;

  }

  <span>private</span><span><span>function</span><span>test2</span><span>()</span>
  {</span>
    sleep(<span>3</span>);
    <span>echo</span><span>2</span>;
    <span>echo</span><span>"<br>"</span>;
  }

  <span>private</span><span><span>function</span><span>test3</span><span>()</span>
  {</span><span>echo</span><span>3</span>;
    <span>echo</span><span>"<br>"</span>;
  }

}

<span>$arr</span>=<span>array</span>(<span>'test1'</span>,<span>'test2'</span>,<span>'test3'</span>);

<span>foreach</span> (<span>$arr</span><span>as</span><span>$fun</span>)
{
    <span>$th</span>[]=<span>new</span> test(<span>$fun</span>);
}

<span>foreach</span> (<span>$th</span><span>as</span><span>$worker</span>)
{
    <span>$worker</span>->start();
}



</span></code>

The output result is
1
3
2

The test class inherits the thread class and needs to implement the run method. The start method will automatically start a thread to execute the program in the run method. For the specific method of thread, please refer to the official manual
http://php.net/manual/zh/class.thread.php

').addClass('pre-numbering').hide(); $(this).addClass('has-numbering').parent().append($numbering); for (i = 1; i ').text(i)); }; $numbering.fadeIn(1700); }); });

The above introduces the use of php thread extension, including aspects of content. I hope it will be helpful to friends who are interested in PHP tutorials.

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn