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

Use of php thread extension

WBOY
Release: 2016-07-29 08:58:59
Original
1856 people have browsed it

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>
Copy after login

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.

Related labels:
source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template