Java multithreaded Programming--why multithreading is required

Source: Internet
Author: User

When it comes to the need for multithreaded programming, it may be necessary to start with the history of the concept of concurrency.

A long time ago, computers did not have an operating system, and at the same time they could only perform a single program, and those programs accessed all the computer resources directly. There was nothing wrong with the way the program was handled in the era when the computer was just published. But with the rapid development of computers and software, operating systems and multi-core processors, this makes the computer less efficient at the same time, and does not give full play to the computer's ability to make the most of the computer's resources. So it becomes inevitable to execute multiple programs at the same time. So what are the advantages of a computer executing multiple programs at the same time?

Resource utilization: Programs sometimes need to wait for external operations, such as input and output, and cannot do any worthwhile work while waiting. It is more efficient to have another program run while the program is waiting.

Fair competition: Multiple users and programs have equal rights to system resources. Having them share the computer through better time slices is preferable to starting the next program after you end a program.

Easy to handle: it's easier and more satisfying to write multiple programs and have each program handle tasks individually and then do the necessary coordination than to write a separate program to handle all the tasks.

In order to make full use of the system resources, so that multiple programs in the process of running a fair competitive resources, the full use of computer processing capabilities, which strongly promoted the process and thread development. Threads allow multiple streams of data in a program control flow to exist simultaneously in the same process. They share resources such as process-scoped memory and file handles, but each thread has its own program counters, stacks, and local variables. Threads also provide a natural decomposition for the parallel use of hardware in multiprocessor systems; multiple threads of the same program can be scheduled simultaneously in multiple CPUs.

a simple introduction to threads

Threads are also a lightweight process, and most operating systems now use threads as a basic dispatch unit. In the absence of a display schedule, each thread is executed simultaneously and asynchronously. Because threads share the memory address space of their processes, all threads may access the same variable and allocate objects on the same heap during execution, which provides a more fine-grained data-sharing mechanism than the interprocess communication (inner-process) mechanism. However, if the shared data does not display a synchronization mechanism, one thread may modify the variable that another thread is modifying, which can result in unexpected results.

Because the basic unit of program scheduling is a thread, a single-threaded application can only run on one processor at a time. In a dual-processor system, a single threaded process discards half of the idle CPU resources, and in a system with 100 processors, the single threaded process discards 99% of its resources. On the other hand, programs that have more than one active thread can run on multiple processors at the same time. With good design, multithreaded programming improves throughput through more efficient use of idle processor resources.

Using multithreading can also help us achieve better throughput in a single-processor system. If a program is single-threaded, the processor is still idle while waiting for a synchronous I/O operation to complete. In a multithreaded program, when one thread waits for the I/O to end, another thread can run, which makes the application still progress when it encounters I/O blocking.

A program that deals with the same type of task in a sequential order is simpler, less error-prone, and easier to test than the need to manage multiple types of tasks at the same time. In the case of simulations, assigning a thread to each type of task, or assigning a thread to each element, provides an ideal order, and doing so separates the domain logic from the details of the timing schedule, alternating operations, asynchronous I/O, and waiting for resources. A complex, asynchronous process can be broken down into a series of simpler synchronization processes, each running in a separate thread and interacting with each other only at specific synchronization points.

Multithreaded programming brings us a lot of advantages, it allows us to do a lot of things at the same time, but in the development of multi-threaded applications, we need to pay attention to a number of issues, such as thread synchronization, communication between threads, thread deadlock, and so on, we will be more in-depth discussion of these issues, learn together, grow together.

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.