5. Java concurrency and multithreading-same thread

Source: Internet
Author: User

The following information is transferred from http://tutorials.jenkov.com/java-concurrency/same-threading.html (using Google Translate):

The same thread is a concurrency model in which a single-threaded system is extended to n single-threaded systems. The result is n single-threaded systems running in parallel.

The same threading system is not a purely single-threaded system because it contains multiple threads. However, each thread runs like a single-threaded system.

Why is a single-threaded system?

You might wonder why anyone would design a single-threaded system today. Single-threaded systems are popular because their concurrency models are much simpler than multithreaded systems. A single-threaded system does not share any data with other threads. This allows a single thread to use non-concurrent data structures and better utilize CPU and CPU caching.

Unfortunately, single-threaded systems are not able to take full advantage of modern CPUs. Modern CPUs typically come with 2 or more 4 cores. Each core can be used as a separate CPU. Single-threaded systems can only use one of the cores, as shown below:

Same thread, single-threaded scaling

To take advantage of all cores in the CPU, you can extend a single-threaded system to take advantage of the entire computer.

One thread per CPU

Systems of the same thread typically run one thread per CPU in the computer. If the computer contains 4 CPUs or 4 cores of CPUs, it is normal to run 4 instances of the same-thread system (4 single-threaded systems). Shows this principle:

No shared status

The same threading system looks like a multithreaded system, because the same threading system runs multiple threads in it. But there is a subtle difference.

The difference between a threaded and multithreaded system is that the threads in the same thread system do not share state. There is no shared memory and the thread accesses it concurrently. There is no concurrency data structure, and so on, thread sharing. The difference is as follows:

The lack of shared state is the behavior of each thread if it is a single-threaded system. However, because the same thread system can contain multiple threads, it is not a true "single-threaded system". Because of the lack of a better name, I found it more accurate to say that the system is an identical threading system rather than a "multithreaded system with single-threaded design". The same thread is easier to explain and easier to understand.

The same thread basically means that the data is kept in the same thread, and the threads in the same-thread system do not share it at the same time.

Load distribution

Obviously, the same thread system needs to share the workload between running single-threaded instances. If not, only one instance will get any work, and the system will actually be single-threaded.

The way in which the load is correctly distributed on different instances depends on the design of the system. Below I will introduce a few.

Single-threaded micro server

If your system consists of multiple micro-servers, each micro-server can run in single-threaded mode. When you deploy multiple single-threaded microservices to the same machine, each micro-server can run a single thread on a single CPU.

MicroServices do not naturally share any data, so microservices are a good example of the same threading system.

Services with Shard data

If your system actually needs to share data, or at least a database, you can split the database. Shards represent the allocation of data between multiple databases. Data is often split so that all data related to each other is in the same database. For example, all data that belongs to an "owner" entity is inserted into the same database. Sharding is not within the scope of this tutorial, so you will have to search for tutorials on that topic.

Thread Communication

If the threads in the same thread need to communicate, they are implemented by message passing. The thread that wants to send a message to thread A can be implemented by generating a message (a sequence of bytes). Thread B can copy the message (a sequence of bytes) and read it. Thread A cannot modify the message by copying message thread B to make sure that thread A is read by threads B. Once it is copied, it is immutable thread A.

Thread communication through messaging is as follows:

Thread communication can be done through queues, pipelines, UNIX sockets, TCP sockets, and so on. Whatever it takes to fit your system.

A simpler concurrency model

Every system running on its own thread in the same threading system can be implemented as a single thread. This means that the internal concurrency model is much simpler than the thread-sharing state. You don't have to worry about all the concurrency problems that can result from concurrent data structures and data structures.

Illustrations

Here are the illustrations of single-threaded, multithreaded, and threaded systems, so you can more easily understand the differences between them.

The first example shows a single-threaded system.

The second example shows a multithreaded system of thread-sharing data.

The third example shows the same thread system with 2 threads that separate the data and communicate by passing messages.

5. Java concurrency and multithreading-same thread

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.