Dark Horse Programmer--Multi-line Cheng (i.)

Source: Internet
Author: User

-----------Android Training, Java training, Java Learning Technology blog, look forward to communicating with you! ------------

The relationship between multi-threading and CPU and memory

From Baidu know.

This answer I think is a good metaphor.

CPU is not at the same time in the process operation, we know that there are many processes in the Windows operating system, and the CPU is a one to operate these processes, of course, the multi-core CPU can be implemented at the same time to operate these processes, so why the more CPU cores the faster the computer is like this, But memory is a bottleneck and cannot exceed this amount of memory.

According to the above mentioned CPU is a one to control the process (high-speed switching), then there will be a problem, if we start multiple threads control the data inside a process, because the CPU is one of the processing, and the thread can be implemented concurrently, In case the CPU is just processing here and jumping to the other process, the thread that is starting is processing the data of the process, then there is a security risk, such as:

2 Threads control data for a process

One thread 1 executes the data of this process when the CPU is just switching, and thread 1 is suspended.

At this point, if thread 2 gets CPU execution, thread 2 executes the process data, and thread 1 is activated to execute the data.

int x = 1;

String name;

int money;

void Run ()

{

if (x > 0)//thread 1 is judged to be suspended, thread 2 entered to determine that the condition was met

{

x = x-1;

Name = "Kvieta, mind";

Money + = 1000;

Core code of the bank

......

}

}

According to the above code combined with the text description, then my account does not have 2000 dollars!

Workaround: Java provides a keyword synchronized, which includes two usages: Synchronized method and synchronized block

Refer to the API for details.

The synchronization function controls the entry and exit of the thread with this, and the synchronous static function controls the access of the thread to the byte-code file corresponding to the class to which the function belongs.

Dark Horse Programmer--Multi-line Cheng (i.)

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.