PHP 7中的同步块 [英] Synchronized block in php 7

查看:56
本文介绍了PHP 7中的同步块的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我来自具有同步块的Java背景:

i come from a java background where there were synchronized blocks:

"Synchronized"关键字可防止同时访问一个 代码或对象由多个线程组成.

The "Synchronized" keywords prevents concurrent access to a block of code or object by multiple Threads.

java中的示例代码:

example code in java:

public void addName(String name) {
    synchronized(this) {
        lastName = name;
        nameCount++;
    }
    nameList.add(name);
}

现在,此示例突出显示了php和java的根本区别(如果我错了,请纠正我).但是php中不存在单例或共享类.因此,当作为单例使用时,给定的Java代码示例非常有意义.因此在请求之间有一个共享的对象.现在,可悲的是,对于php似乎并不存在这种情况,这显然是一个主要缺点.但是要以php的方式进行操作,最终将把名称计数写入文件或数据库中,从而以这种方式在请求之间共享数据(显然要慢得多).但是问题是一样的:如果有2个请求在同一时间增加名称计数,那将是一个太少了.

Now this example highlights a fundamental difference in php and java (correct me if i am wrong). But a singleton or shared class does not exist in php. So the given example of the java code makes a lot of sense when using as a singleton. Thus having a shared object between requests. Now this does not, sadly, seem to exist for php, a major disadvantage obviously. But to do it the php way, that would eventually be writing the name count into a file or database, thus having shared data between requests that way (a lot slower obviously). But the problem would be the same: if 2 requests increase the name count at the exact same time, it will be one too little.

现在第一个问题:php 7是否存在类似的东西?即同步块

Now the first question: does something similar exist for php 7? that is, the synchronized block

现在我不确定在php 7中线程这个词是否真的适用于我担心的问题.是php中的一个线程是否也被认为是对php文件的单独调用,可以说foo.php,即,如果我同时两次访问foo.php,则同步块(如果存在)将仅在执行一次后执行另一个,还是我必须通过扩展类Thread来创建适当的php线程,然后才将其视为线程?

Now i am not sure if in php 7 the word thread really applies to what I am worried about. Is a thread in php considered to be as well a seperate call to a php file lets say foo.php that is, if I access foo.php at the same time twice, will the synchronized block if it exists, be executed only one after another, or do I have to create a proper php thread by extending the class Thread and only then it counts as thread?

推荐答案

您的问题的简短答案是否",对于PHP而言,这不存在任何问题,因为正如您所指出的那样,PHP无法运行多线程进程.在这方面,PHP 7与以前的PHP版本相同.

The short answer to your question is No, nothing like this exists for PHP because, as you point out PHP is does not run multi-threaded processes. PHP 7 is the same in this respect as previous PHP versions.

现在,您将缺少多线程描述为主要缺点.不一定是这种情况:这是主要的差异.是否不利是另一个问题.这很大程度上取决于上下文.

Now you describe the lack of multithreading as a major disadvantage. This isn't necessarily the case: it's a major difference. Whether it's a disadvantage or not is another question. That depends very much on context.

您描述的问题是进程之间具有共享对象.共享对象是没有多线程的PHP的必修课,但是共享对象的要点是在对象内共享 data .

The problem you describe is of having shared object between processes. A shared object is a non-sequitur for PHP without the multi-threading, but the main point of a shared object is to share the data within the object.

如果我们正在谈论共享数据,那么您很正确地认为数据库或文件是实现此目的的常用方法,并且通常在性能方面就足够了,但是如果您确实需要更多性能,则可以真正共享通过使用Memcache之类的数据在内存中存储数据.有完善的库可以处理PHP中的内存缓存.这将是正常的PHP解决方案.

If we're talking about shared data, you're right that a DB or file is a common way to do that, and is usually sufficient in terms of performance, but if you really need more performance you can genuinely share the data in memory by using something like Memcache. There are well-established libraries for dealing with memcache in PHP. This would be the normal PHP solution.

现在我想在这里提出另外两件事,这可能与这里有关.

Now there are two other things I'd like to raise here that may be relevant here.

首先,让我将NodeJS添加到方程式中,因为这又使事情有所不同.在NodeJS中,系统也是单线程的,但是与PHP会为每个请求启动一个新进程的PHP不同,在NodeJS中,所有请求都被馈送到一个不断运行的线程中.这意味着在NodeJS中,即使它们是单线程的,您也可以拥有在请求之间共享的全局数据(通常是DB连接),因为它们都在同一进程中运行.

Firstly, let me add NodeJS to the equation, because this does things differently again. In NodeJS, the system is also single threaded, but unlike PHP which starts a new process for each request, in NodeJS all requests are fed into a single constantly-running thread. This means that in NodeJS, even though it's single-threaded, you can have global data (commonly the DB connection) that is shared between requests, because they're all running in the same process.

这里的要点是,单线程并不是PHP无法在请求之间共享数据的原因.更重要的是,在PHP中,每个请求都在自己的过程中与其他请求隔离开来.这实际上不是一个缺点,而是实际上是一个优点-例如,PHP崩溃不会破坏您的整个站点,而在多线程或共享线程环境中,这样做可能会导致整个站点崩溃.实际上,这是NodeJS的最大弱点之一:一点点写得不好的代码很容易使服务器完全无响应.

The point here is that being single-threaded isn't the reason why PHP can't share data between requests; it's more about the fact that in PHP each request is isolated from the others in its own process. Far from being a disadvantage, this can actually be an advantage -- for example, a PHP crash won't take down your entire site, where it could do in a multi-threaded or shared thread environment. In fact this is one of NodeJS's biggest weaknesses: it's quite easy for a single bit of poorly written code to make the server completely unresponsive.

我想提出的第二件事是,实际上有PHP的实验分支,实际上允许您在多线程和共享线程环境中使用该语言.在这两种情况下,这些都是非常实验性的,当然不应该用于生产.正如您在问题中指出的那样,该语言缺少在这些环境中使用所必需的关键功能.但是事实是可以做到的.

The second thing I wanted to raise is that there are in fact experimental branches of PHP that do in fact allow you to use the language for both multi-threading and shared-thread environments. In both cases, these are very very experimental, and certainly should not be used for production. As you note in the question, the language is missing key features that would be necessary for use in these environments. But the fact is that it can be done.

由于现有的PHP代码并未考虑到此类环境,因此这些实验都不会实际进行.您已经知道如果在不保护共享数据的情况下编写多线程Java程序会发生什么,那么应该清楚,如果PHP曾经认真考虑过在带有共享数据的平台上运行,那么任何想与现有PHP一起使用的人代码将需要进行大量的重写.这不仅会发生,因此可以肯定地说,PHP将使用隔离进程的当前格式来坚持使用.

Neither of these experiments is ever going to actually go anywhere because existing PHP code is not written with these kinds of environments in mind. You'll already know what happens if you write a multi-threaded Java program without protecting your shared data, so it should be clear that if PHP were ever to seriously entertain running in platforms with shared data then anyone wanting to use it with existing PHP code would need to do extensive rewriting. That's not just going to happen, so it's safe to say that PHP will stick with it's current format with isolated processes.

这篇关于PHP 7中的同步块的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆