Java multithreading concurrent programming and CPU clock allocation

Source: Internet
Author: User

Let's take a look at the relationship between multithreaded programming and CPU clock oscillation in Java.

The usual, first science

Our operating system was a single mission before DOS.

What is a single task? Only one thing at a time.

When you copy a file, you can't rename it.

So now the operating system, I am on the side to write a blog, while listening to music, while opening QQ, one side .......

Obviously, the current operating system is a multitasking operating system.

What is the operating system support for multitasking?

Start a process for each program you open, assigning it the appropriate space (primarily the memory space of the running program)

This actually supports running concurrently.

The CPU has a clock frequency that indicates the number of times a CPU instruction can be executed per second,

In each clock cycle, the CPU can actually execute only one (or more) instructions.

The operating system manages the process threads,

Assign a short period of time (not necessarily evenly divided) for each process in turn (not sequentially)

Then within each process, the program code handles the time allocation of the internal threads of the process itself,

Multiple threads switch between each other to execute,

Because the switching time is very, very short.

So, the feeling is that multiple tasks, multiple threads are running concurrently

In fact, from a microscopic point of view,

The operation of the program is still asynchronous.

Since it's all running asynchronously,

So

Our multi-threaded operation

In fact, it's just that the CPU is just running serially.

There's no running at all?

Since this

So what's the point of our multithreading?

And we know that.

The CPU is running too fast.

But the reading speed of our I/O

The transmission of the network is familiar

Connection to the database--read speed

The speed of our operation

These are compared to the speed of CPU operation.

It's too slow, it's too slow.

So

When we do these things,

In fact, the CPU is still idle in most cases

In this case

We run multiple tasks on multiple threads

So when process a needs IO and so on

CPU idle to process B related operations

Instead of having to run thread B when thread a runs out as a serial

This also increases the CPU utilization

Some children's shoes may be seen in certain forums or places.

Discussion and argument about how Java multithreading slows down CPU operation

What's going on here?

In fact, the key to this problem is:

What are you going to do with the CPU?

If you let the CPU do all the internal operations,

(No I/O network database access, etc...)

So, artificially hard to break the CPU internal operation

Used to give multiple threads a "tick" operation

Then the CPU can only be slower

But

If your business is not all CPU-internal operations

and network \ Database access \i/o (such as guimouse operations), etc.

There are obvious advantages to using multithreading

Said so much.

It's not the most important question.------The purpose of multithreading

The purpose of multithreading is to maximize the use of CPU resources

All right

So we're going to talk about CPU issues.

In the CPU

frequency = FSB x octave

Higher frequency

The more instructions you can complete within a clock cycle

Of course, the speed of the CPU will be faster.

Previous methods to increase CPU operation speed

is to increase CPU frequency

But with the development of time

CPU frequency to some extent

has reached the physical limit, it is difficult to improve the

So what should we do if we want to continue to improve CPU performance?

We know

Traditional CPU

Only one kernel

This kernel is also used to run only one thread at a time

Now to improve CPU performance

We can do it on one core of the CPU.

Allow multiple operations to run

Multi-threaded concurrency running from the hardware level

In order to improve the computing performance of the CPU

Only use multicore CPUs with Hyper-Threading technology

-------------I think that's the long-term meaning of Java support for multithreaded concurrency

Said so much.

Let's go back to our theme.

Multithreading in Java

1. About Memory

I've said it before.

Under the operating system

Each program opens

The system will start a process

assigning system resources to processes (memory required to run programs, etc.)

In Java

All threads are owned by the JVM Scheduler

So

What are the similarities and differences between the threads in Java and the processes under the system in memory usage?

And all the variables in Java are stored in main memory.

Shared for all threads (in the same process)

Each thread has its own working memory (working memories)

Working memory is a copy of some of the variables in main storage

The operation of a thread on all variables is performed in working memory.

Threads cannot be accessed directly from each other

Variable delivery is done through main memory

But inside the program you can call each other (through the object method)

Communication between all threads is relatively simple and fast

Inter-process internal data and state are completely independent of each other

In most cases, inter-process communication must be implemented over the network

The data for the thread itself is usually only the register data

And a stack used by a program when it executes

So thread switching is less burdensome than process switching

About thread Scheduling

I said it earlier.

CPU scheduling for individual threads is random (time-sharing scheduling)

In a Java program, the JVM is responsible for thread scheduling

Thread scheduling refers to the allocation of CPU rights to multiple threads------by a specific mechanism

There are two types of scheduling modes:

Time-sharing scheduling and preemptive scheduling.

The time-sharing scheduling is that all threads take turns to gain CPU access, and evenly allocate each thread to occupy the CPU;

Preemptive scheduling is based on the priority level of the thread to obtain the use of the CPU.

The JVM's thread-scheduling pattern employs a preemptive pattern.


Since it's a preemptive dispatch,

Then we can set the priority to "limited" control of the running order of threads

Note "Limited" once

Java multithreading concurrent programming and CPU clock allocation

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.