Java Thread Multithreading Introduction

Source: Internet
Author: User

1. Threading Overview

    Almost all operating systems support running multiple tasks at the same time, and a task is usually a program, and each running program is a process.

    When a program runs, the internal may contain multiple sequential execution flows, and each sequential execution flow is a thread.

2. Threads and processes

    Almost all operating systems have a process concept, and all running tasks usually correspond to a process. When a program goes into memory, it is a process.

    Process is a running program, with a certain degree of independence, process is a system of resource allocation and scheduling of an independent unit.

  Process features:

    A, independence: The process is a separate entity in the system, can have its own independent resources, each process has its own private address address.

            A user process cannot access other process address spaces without the process itself allowing it to.

    B, Dynamic: The difference between a process and a program is that the program is just a static set of instructions, and a process is a collection of instructions that are active in the system.

            The concept of time is added to the program, and the process has its own life cycle and a variety of different states, which are not available in the program.

    C, Concurrency: Multiple processes can execute concurrently on a single processor, with no interaction between multiple processes.

    Multi-Threading extends the concept of multiple processes, allowing the same process to concurrently handle multiple tasks simultaneously. Threads are also known as lightweight processes (lightweight process), which are the execution units of a process.

Just as the process is in the operating system, threads are independent and concurrently executing streams in the program. When the process is initialized, the main thread is created. For most applications, there is usually only one main thread,

But we can also create multiple sequential execution flows within the process, which are threads and each thread is independent of each other.

    A thread is part of a process, a process can have multiple threads, and a thread must have a parent process. A thread can have its own heap, stack, program counter, local variable, but cannot have system resources,

It shares all of the resources of the process with other threads of the parent process. Because multiple threads share all of the resources of the parent process.

    Threads can accomplish certain tasks, share variables in the parent process with other threads, and part of the environment, and collaborate with each other to complete the tasks that the process needs to accomplish.

    A thread is run on its own, and it does not know if any other processes exist in the process. The execution of a thread is preemptive, that is, the currently running thread can be suspended at any time for other threads to run.

One thread can create and revoke another thread, and multiple threads in the same process can execute concurrently.

Summary: After a program runs at least one process, a process can contain multiple threads. Contains at least one thread.


3. Concurrency and parallelism

    concurrency (concurrency) and parallelism (parallel) are two concepts;

    Parallel means that at the same time, multiple instructions (threads) are executed concurrently on multiple processors;

    Concurrency means that only one instruction (thread) can execute at the same time, but multiple process instructions are executed quickly, making the effect of multiple processes concurrently executing on the macro.

4. Advantages of Multithreading

    The thread partition scale is smaller than the process, which makes multithreading high concurrency. The process has its own independent unit at execution time, and multiple threads share memory, which improves operational efficiency.

    Threads have higher performance than processes because threads in the same process have commonalities: multiple threads share the same process's virtual space.

    Thread commonality environments include: Process code snippets, process common data, and so on. It is easy for threads to communicate with common data.

    When the operating system creates a process, it must give the process separate memory space and allocate a large number of related resources, but it is much simpler to create a thread, so multithreading to achieve concurrency is much better than concurrency for multi-process implementations.

Multithreading Benefits:

    A, the process cannot share memory, but sharing memory between threads is easy.

    B, the system creation process needs to reallocate system resources for the process, but creating threads is much less expensive, so using threads to achieve multitasking is more efficient than multiple processes.

    C, the Java language built-in multi-threaded function support, rather than simply as the underlying operating system scheduling, so as to simplify the multithreaded programming of Java.

Java Thread Multithreading Introduction

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: 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.