Basic concepts of Java threading

Source: Internet
Author: User
Tags lock queue

The birth of process and thread processes

Operating system has 2 tasks, a, a first execution, execution to half of the need Io, so a lot of time, in this period of time the CPU is idle, wasting resources, so there is a process, when a temporarily unable to use the CPU, but can not be destroyed, it temporarily saved up, let B to execute. b after execution or need a to execute, according to the information of the temporary recovery come over.


Each process corresponds to a certain amount of memory space, and can only use its own memory space, and preserves the running state of the program, which also provides the basis for process switching.

The birth of a thread

First, the emergence of multicore processors, in order to better utilize multi-core processor, to avoid the waste of resources. The second procedure requires that people perform tasks concurrently in a program. For example, the player side plays video, we also want to be able to side product theory, obviously to concurrent execution. Third. Blocking (IO) in one program can prevent the program from continuing, which affects the experience and the operation, so we need multi-threading to throw the time-consuming operations into the sub-threads, so the program will continue to go on.

Basic concepts and points of attention for threads

1. For the operating system, the basic unit of resource allocation is the process, and the basic unit of dispatch is the thread.


2. For a program, not necessarily multi-threading efficiency is high, can not blindly use, multi-threaded use and specific business scenarios (such as interaction), as well as the characteristics of the machine, such as multi-core or single-core machine, etc.


3. Each thread represents a separate execution flow, has its own program counter, has its own stack, but the threads can share memory, and they can access and manipulate the same objects.


4.java is a single-threaded programming language, if you do not actively create threads, then the default is only the main thread, of course, the JVM will have many other daemon threads exist, such as garbage collection
(Erlang is a concurrent programming language, and each function can be driven as a separate task)

The state of the Java thread


1. NEW: Create a Thread object


2. Can run: The thread invokes the Start method, which exists in the pool of running threads.


3. Running state: The thread gets the CPU usage.


4. Blocking states: The Wait () method of the calling object of the running thread (the method exists only in the synchronous lock code block), yielding the lock and entering the waiting queue. Use the Notify () method only with the same thread that called the object to enter the lock pool. Second: When the running thread acquires a locked resource, if the resource is occupied, it goes into the lock queue and only gets into the operational state when the thread in the lock queue acquires the locking object resource. Third: The running thread executes the sleep () or join () method, or IO, the thread blocks the queue, waits for a time, or IO completes, and automatically enters the operational state.


5. Death: The thread finishes executing, or an abnormal interrupt exits.

Basic concepts of Java threading

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.