Static variables and concurrency

Source: Internet
Author: User
Today I suddenly came up with a question about static variables, and my former manager also said that the program should use as few static keywords as possible. I understand it like this at present, because I have also read the concept of concurrency before. you see, static is generally said that all objects share the same, isn't it... today I suddenly came up with a question about static variables, and my former manager also said that the program should use as few static keywords as possible.
I understand it this way, because I have also read the concept of concurrency before. you can see that static is common to all objects. isn't it concurrent, multiple users will share some variables, such as setting the number of purchased items to static, so that they will not be able to tell which user has actually purchased many items. I think this example is not very good, but it probably means that if all objects share the same ownership, no object needs to be differentiated? Is there any hero who can answer my questions or give me a practical application scenario? Thank you again.

Reply content:

Today I suddenly came up with a question about static variables, and my former manager also said that the program should use as few static keywords as possible.
I understand it this way, because I have also read the concept of concurrency before. you can see that static is common to all objects. isn't it concurrent, multiple users will share some variables, such as setting the number of purchased items to static, so that they will not be able to tell which user has actually purchased many items. I think this example is not very good, but it probably means that if all objects share the same ownership, no object needs to be differentiated? Is there any hero who can answer my questions or give me a practical application scenario? Thank you again.

I think you understand the problem:
First, each http request, such as reverse proxy to fastcgi server through nginx, is parsed and executed by different requests in different php-fpm processes, each process (the basic unit for system resource allocation and scheduling) is independent of each other from exclusive memory;
Second, static attributes are directly loaded into the memory (heap) when the script is executed (theoretically higher than the efficiency of creating an object and then calling the attributes );
Therefore, the static property is not related to concurrency, but the proper use of the static property will theoretically increase the script execution speed, so it will reduce the execution time of the script unit, this improves the system's ability to withstand stress during concurrency.

Extra: the concurrency of php depends theoretically on the number of php-fpm processes.

Static is the class holding, not the object holding. at this time, the class has not been instantiated and there are no objects, let alone all objects.

Static variables are class attributes, meaning that all objects of this class are shared. for example, the number of train tickets of a certain train trip can be purchased by users across the country, but how can we determine the remaining number of votes on the train ticket? One of these implementations can be implemented through static variables. Define the remaining quantity of train tickets as static. each time someone buys a ticket (different objects), modify this attribute synchronously (static-1)
You can check the meaning of the static keyword.

Let me say a few unfriendly words.
What are your answers. I think you should make your own judgment before adopting the answer.
Or read the document first.

The correct statement @ TIGERB is clear.

Each HTTP request is in exclusive memory.

Static is not closely related to concurrency. Then I will reference TIGERB's sentence.

However, the proper use of the static attribute will theoretically speed up script execution.

Obviously, you don't have to think too much about this theoretical improvement.
Concurrency should be considered more
1 network IO (http request, TCP, IP handshake time ).
2. database I/O and concurrent read/write congestion. A large amount of data uses correct and appropriate indexes for read/write splitting.
3. use cache.
4.
It has nothing to do with this staic.

Just answer your last question:

Or an actual application scenario

Before that, I think the subject should understand the differences between static, $ this, and self in PHP.

For example, static can be used in design mode-static factory mode. For specific design patterns, the subject can refer to others' articles.

The difference is as follows:

class A {    public static function get_self() {        return new self();    }    public static function get_static() {        return new static();    }}class B extends A {}echo get_class(B::get_self());  // Aecho get_class(B::get_static()); // Becho get_class(A::get_self()); // Aecho get_class(A::get_static()); // A

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.