JAVA thread ---- basics, java thread ----

Source: Internet
Author: User

JAVA thread ---- basics, java thread ----

Thread introduction:

Nowadays, the personal computer operating system usually adopts multi-task and time-based design. Generally, an operating system that can execute multiple programs within the same time has the concept of a process. A process is an execution program, and each process has its own independent memory space and a group of system resources. In the process concept, the internal data and status of each process are completely independent. Java programs use Flow Control to execute program streams. A single sequential flow control in a program is called a thread. multithreading means that a single program can run multiple different threads simultaneously to execute different tasks. Multithreading means that multi-line statements of a program can run almost at the same time.

A thread is similar to a process. It is a piece of code that completes a specific function. It is a flow control in an order in a program. But unlike processes, multiple threads of the same type share a piece of memory space and a group of system resources. The data of the thread itself is usually only the register data of the microprocessor, and a stack used for program execution. Therefore, when the system generates a thread or switches between threads, the burden is much lower than that of the process. Because of this, the thread is called a lightweight process. A process can contain multiple threads.

A thread is the sequential control flow within a program.

(1) process: each process has its own code and data space (process context), and the overhead of process switching is high.

(2) threads: lightweight processes. The same class of threads share code and data space. Each thread has an independent running stack and program counter, and the thread switching overhead is small.

(3) multi-process: Multiple Task programs can be run simultaneously in the operating system;

(4) multithreading: multiple sequential streams are executed simultaneously in the same application;

 

Thread status:

Java uses the object of the Thread class and its subclass to represent a Thread. A Thread usually goes through the following four States in a complete life cycle:

1. Creation status

When a Thread class or its subclass object is declared and created, for example, when the following statement is executed, the Thread is in the Creation state:

Thread myThread = new MyThreadClass ();

When a thread is being created, it only allocates memory resources and cannot be executed.

2. Running status

Only the start () method can be called for a thread object to enter the running state;

MyThread. start ();

When the thread is running, the system allocates the required system resources to the thread, schedules the thread to run and calls the thread running method.

3. Not running

A non-running status is also called a blocking status. For some reason (input/output, waiting for a message, or other blocking situations), the system cannot execute the thread status. There are several reasons why a thread enters this status.

(1) The sleep method is called (2) and the suspend method is called.

(3) The thread calls the wait method to wait for a condition variable.

(4) the input/output stream is blocked.

4. Death status

The thread releases the entity. A thread in the dead State does not have the ability to continue running. It can be implemented in two ways:

(1) undo it naturally. A normal running thread completes all of its work, that is, the thread execution is completed.

(2) The thread is stopped and the stop method is called. This method is generally not recommended to terminate thread execution.

 

The programs we previously wrote are executed in a certain order from top to bottom. After the thread is introduced, this harsh rule is broken and the program will open up multiple branches to run, coupled with the high-speed data processing capability of the processor, we seem to be running at the same time.

 

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.