Java multithreading (I) basic knowledge and concepts, java Multithreading

Source: Internet
Author: User

Java multithreading (I) basic knowledge and concepts, java Multithreading
Multi-Thread basic Thread Concept

  ThreadIt is the flow control in separate order in the program.

The thread itself cannot run. It can only be used in a program.

Note: A thread is the sequential control flow in a program. Only resources and environments allocated to the program can be used.

Process

  Process: The program being executed.

A program is a static concept and a process is a dynamic concept.

A process can contain one or more threads.

A process must contain at least one thread.

Differences between threads and processes

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 the thread is usually only the register data and the stack used when a program is executed.Thread switching is less costly than process switching..

Multi-threaded programs require less management costs than multi-process programs.

Processes are heavyweight tasks and need to be allocated to them with independent address spaces. inter-process communication is expensive and restricted, and inter-process conversion is also costly.

On the other hand, threads are lightweight players who share the same address space and share the same process together. Communication between threads is cheap, and inter-thread conversion is also low-cost.

Single thread

Only one thread in a single program is a single thread.

When the program starts to run, a thread is automatically generated, and the main method is in thisMain thread. Our programs are all executed by threads.

 

Multithreading

  MultithreadingA single program can run multiple different threads to execute different tasks at the same time.

  Multi-threaded programmingThat is, "maximize the use of CPU resources". when processing a thread does not need to occupy the CPU but only deals with IO and other resources, this gives other threads that need to occupy CPU the opportunity to obtain CPU resources. Basically, this is the ultimate goal of multi-threaded programming.

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 the task later and another task later.

From a macro perspective, multiple threads are executed simultaneously (macro parallel). However, the number of processors determines the maximum number of threads that can run simultaneously at a certain time point, for example, a single-core CPU can only have one thread to execute (micro-serial) at a certain time point. A dual-core CPU can run up to two threads at a certain time point to achieve micro-parallel.

Multithreading in Java

Different from most other programming languages, Java supports multi-threaded programming (Multithreaded Programming).

A multi-threaded program contains two or more concurrent running parts, each of which is calledThread ).Each thread has an independent execution path, so multithreading isMulti-task processing.

Multi-task processing is supported by all modern operating systems. However, there are two completely different types of multitasking:Process-basedAndThread-based.

  1. process-based multitaskingIs a more familiar form.Process)It 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 multitasking,ProgramIs the smallest unit of code assigned by the scheduler.

  2. thread-based multi-task processingEnvironment,ThreadIs 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.

Java thread model

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.

Multithreading allows a live loop to sleep for one second between each frame without suspending the entire system.

 

Thread group

All threads belong to one thread group. It can be a default thread group or a group explicitly specified when a thread is created.

Note:

At the beginning of creation, threads are limited to one group and cannot be changed to a different group.

If you create multiple threads without specifying a group, they will belong to the same group as the thread that created it.

 

References

Shengsiyuan Zhang Long's Java SE series video tutorial.

 

Source: http://www.cnblogs.com/mengdd/archive/2013/02/16/2913628.html

 

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.