A humble opinion of Java and CPU

Source: Internet
Author: User

As a Java programmer, we should also know how the computer works and how the computer is doing it. In fact, I regret that sophomore time did not learn the knowledge of the bottom of the computer, most of the time spent on those huaquanxiutui.

First, the basic unit of the CPU is a thread, we write the program through the process or thread request CPU processing, the CPU will allocate processing time slices, processing time to pass the operand to the CPU, the final processing results output to ' Local variables ', this ' local variable ' in time we say often ' Stack ' (Advanced post-out). In order to improve performance those data will be cached in cache, we know that the cache has a first level cache, a level two cache, and a level three cache. The largest capacity is the level three cache, which is a few 10 trillion. Since the cache interacts with the CPU the fastest why is the capacity so small? Baidu Encyclopedia's explanation is: "Because of the first-level cache technical difficulty and the highest manufacturing costs, increase the capacity of the technical difficulties and increase the cost is very large, the performance improvement is not obvious, the cost is very low", and if the cache capacity is too large, like memory will involve a very complex algorithm management.

We know that Java is able to achieve "write once, run everywhere" because there is a powerful JVM, when the program runs through Java virtual instructions to complete the data and objects in the Java VM Some operations, and finally according to different OS to translate these virtual instructions into the actual OS instructions.

Now, most computers are multicore, just as many people are dealing with a task. So the question is, how does the CPU know when a request comes in? What CPU is processed when the request is initiated? One piece of data is simultaneously operated by multiple CPUs, how to notify other CPUs what did I do?

My idea for the first question is similar to the keyboard, the listener constantly scanning the keyboard to listen, if the CPU will be very busy and inefficient, then there is "interrupt technology", with its CPU does not have to ask whether there is a request, but need to request the processing device sent a "request Interrupt" Signal, then the CPU will drop the work at hand to handle the request. The complete process is: request interrupt → response interrupt → close Interrupt → leave breakpoint → interrupt source identification → protection field → Interrupt service sub-program → resume scene → interrupt return.

The second question my idea is that a request to process the CPU to control all the request operations, there are some drawbacks, such as: when the volume of the request is very large, this CPU is estimated to be too much. So different CPUs deal with different areas, using the method of dividing the area. This also has drawbacks: different regions may communicate with each other.

Third, the CPU processing speed is very fast, here there is a Cache Line,cache one line or more rows. Example: If you want to traverse a two-dimensional array int[][] a=new int[5][10]; There are two ways to traverse we often use the first dimension as the outer loop, the second dimension as the inner loop, and the second, the other way, is the efficiency of the two methods consistent? I usually do not know how to read with the CPU when the gap. Just now the cache line is read sequentially into the contiguous memory, the Java array allocates memory is first to first dimension, and then allocates the second subspace array, for example: Int[2][1] and Int[3][1] is located on a different array, so with the cache Line to cache into the CPU cache time will be continuously read into multiple, if the second method read in, the data in different arrays in the memory of the region is different, then the cache line will not play his role, and finally will be one of the read into, become very slow.

Four, when the data and cache into the buffer, if multiple CPUs need to process this data, how to maintain the read and write consistency of each CPU? There is a Mesi protocol (cache conformance protocol). Multiple CPUs connected through the bus, each CPU in addition to processing their own requests to listen to other CPU on the bus read and write operations, by listening to their own cache to do the corresponding processing, forming a virtual share. Mesi I am not very clear, only know a few basic rules, 1. A CPU load memory is dedicated; 2. When another CPU loads the same piece of memory, the information held by the two CPUs is shared at this point.

CPU-related and concurrent operations, concurrency has not been seriously studied, has always felt that they do not use, in fact, the concurrency everywhere, just a lot of framework to help us deal with. Next time talk about concurrency, writing is very rough, mainly to comb ideas and share some important things.

A humble opinion of Java and CPU

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.