Java Basics-Multithreading

Source: Internet
Author: User

Introduced

The operating system can run several programs at the same time, and each program that runs independently is called a process .

For the same program, it can be divided into a number of separate execution streams, which we call threads. the ability of a thread to handle multiple tasks

  It is a common method to study software using the view of process and thread, and the emergence of the concept of process and thread is of great significance to improve the parallelism of software. Today's large-scale application software is not a multi-threaded multitasking, single-threaded software is unthinkable. So mastering multithreaded Multi-tasking design methods is a must for every programmer.

Understanding Threads

To explain a thread, you have to talk about the process, which is the execution instance of the application, and each process consists of a private virtual address space, code, data, and other system resources. The resource that the process creates at run time dies as the process terminates

The basic idea of a thread is simple, it is a separate execution flow, a separate execution unit within the process, equivalent to a subroutine

When a single executing program runs, a main thread is included by default, the main thread appears as a function address, and the provider's start point, such as the main () function. When the main thread terminates, the process also terminates.

Depending on the actual needs, the application can be decomposed into many independently executed threads, each of which runs in parallel in the same process.

All threads in a process are in the virtual address space of the process, using the process's global variables and system resources. The operating system assigns each thread a different CPU time slice, and at some point, the CPU executes only one thread within a time slice, and the corresponding threads in multiple time slices are executed in rotation on the CPU, because each time slice is short, so it seems to the user that each thread is processed in parallel on the computer. The operating system is based on the priority of the thread to schedule the CPU time, priority high thread priority, the lower priority thread continues to wait.

Threads are divided into two types:

User interface threads and worker threads (also known as background threads)

User interface threads are typically used to process user input and respond to various events and messages, in fact, the main execution thread of the application CWinApp object is a user interface thread, when the application starts automatically created and started, as well as its termination also means that the end of the program, the process is terminated.

A worker thread is used to perform a program's background processing tasks, such as computing, scheduling, reading and writing to the serial port, and it differs from the user interface thread in that it does not derive from the thread class and is most important for it to implement the running control function of the worker task.

The worker thread and the user interface thread start by invoking different versions of the same function; Finally, it is necessary for the reader to understand that all threads in a process share the variables of their parent process, but at the same time each thread can have its own variables.

Java Basics-Multithreading

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.