Multithreading 1, multithreading java

Source: Internet
Author: User

Multithreading 1, multithreading java

A thread is the flow control in a separate sequence in the program. The thread itself cannot run. It can only be used in a program. A thread is the sequential control flow in a program. It can only use resources and environments allocated to the program.

Multithreading means that multiple threads can run different tasks simultaneously in a single program.

Unlike most other programming languages, Java supports multithreaded programming ).

The purpose of multi-threaded programming is to "maximize the use of CPU resources". when processing a thread does not need to occupy the CPU but only deals with resources such as I/O, this gives other threads that need to occupy CPU resources the opportunity to obtain CPU resources. Basically, this is the ultimate goal of multi-threaded programming. When the program starts to run, a thread is automatically generated, and the main method runs on this main thread. A process can contain one or more threads. Multiple Threads need to be generated when multiple codes are run in a program at the same time. The CPU randomly takes time out, so that our program will do this at a time and another thing at a time.

Multi-task processing is supported by all modern operating systems. However, there are two completely different types of multitasking: process-based and thread-based. Process-based multitasking is a more familiar form. A process is essentially an execution program. Therefore, process-based multitasking features that allow your computer to run two or more programs at the same time. For example, process-based multitasking enables you to run the Java compiler simultaneously when using a text editor. In process-based multi-task processing, a program is the smallest unit of code assigned by the scheduler. In a thread-based multi-task processing environment, a thread is the smallest execution unit. This means that a program can execute two or more tasks at the same time. For example, a text editor can format text while printing.

The internal data and status of multiple processes are completely independent, while multithreading shares a piece of memory space and a group of system resources, which may affect each other. The data of a thread is usually only the register data and the stack used for execution of a program. Therefore, thread switching is less costly than process switching.

The advantage of Java multithreading is that the main loop/polling mechanism is canceled. A thread can be paused without affecting other parts of the program. For example, the idle time generated when a thread reads data from the network or waits for user input can be used elsewhere. Multithreading allows a live loop to sleep for one second between each frame without suspending the entire system. Java runtime systems depend on threads in many aspects. All class libraries are designed with multithreading in mind. In fact, Java uses threads to make the entire environment asynchronous. This helps reduce the invalid part by preventing the waste of CPU loops.

 

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.