Java static concurrency Problem

Source: Internet
Author: User

1.
As long as your static method does not access global variables, there is no concurrency problem
There is no doubt that accessing global variables will cause concurrency problems.

Variables inside static methods are local variables, and memory space is redistributed each time a static method is called, so it is safe. That is, if your static method does not access global variables, there will be no concurrency problems

Java Code Validation Experiment I have always wanted to do, but there is no time, if any interested, can provide a bit.

2.

Concurrency means that multiple tasks can be performed concurrently.
He has nothing to do with static, it's a thread-related, concurrency-safe in Java I personally think it is mainly two points:
1, data resource competition, that is, multiple threads access the same resource, will cause security problems;
2, atomic operation, that is to say I have a series of operations to be done, but no other threads in the middle affect my operation.

Data Resource competition: There are 2 variables in Java, member variables, local variables
A local variable does not have a security problem because the local variable is in the method, and each time the method executes is a separate data that is not accessed by another thread
Member variables can affect concurrency security, such as: non-static member variables are owned by each object itself, and if a method of this object accesses its own member variable, when the object is called by different threads multiple times, do you say that there is a problem? Static member variables are even more so, and any object of that class can be called.

Atomic operations: Atomic simplicity is that a series of operations or processes must be a whole, otherwise it will cause accidents. For example: Many people in the store to buy the same thing, from the pay, take the goods, settlement must be a whole, if someone has bought this thing, another person is in the payment stage, then there will certainly be a problem, but if the three operations are a whole there will be no problems, only one person completely executed these 3, The other person can execute, and the second is not allowed at the same time.

Of course, I'm not necessarily accurate, this concurrency is very complex, you can first understand the approximate
Java to solve concurrency security will sacrifice performance, but you know, 1, not all concurrency security needs to be solved, some in the actual demand is allowed, 2, concurrency security issues, not necessarily rely on concurrency security to solve, there may be design flaws. 3, concurrent access to the same resource does not necessarily cause problems, such as: only one thread in the write operation or all read operations.

Java static concurrency Problem

Related Article

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.