Software Construction (review)--some knowledge of multithreading

Source: Internet
Author: User
Tags thread class

write in front : Understanding multithreading is very necessary, this blog is my knowledge of the point of understanding (also read some of the relevant blog), to review and consolidate the relevant knowledge.

One, what is multithreading?

Multithreading refers to the technique of implementing concurrent execution of multiple threads from software or hardware. Computers with multithreaded capabilities can perform more than one thread at the same time because of hardware support, thereby improving overall processing performance. In a program, these stand-alone program fragments are called "threads", and the concept of programming with it is called "Multithreading (Multithreading)".

Multithreading is to accomplish many tasks synchronously, not to improve the efficiency of the operation, but to improve the efficiency of the use of resources to improve the efficiency of the system. Threads are implemented at the same time when multiple tasks need to be completed.

Second, the life cycle of the thread

1. Newly created (new Thread)
When an instance (object) of the thread class is created, the thread enters a new state (not started).

2. Ready (runnable)
When the thread object calls the start () method, the thread enters the ready state. The ready state thread is in the ready queue, waiting for the thread scheduler in the JVM to dispatch.

3. Operation (running)
The thread obtains the CPU resources that are executing the task (the Run () method), at which point the thread will run until the end, unless the thread automatically discards the CPU resources or has a higher priority thread, which is a complex state.

4. Clogging (blocked)
For some reason, the running thread is getting out of the CPU and pausing its own execution, which goes into a blocked state. You can re-enter the ready state after the sleep time has arrived or the device resource has been acquired. The state can be divided into three types:

4.1 Wait for blocking: the thread in the running state executes the wait () method, which causes the thread to enter a wait-blocking state.

4.2 Synchronous blocking: The thread acquires a synchronized synchronization lock failure (because the synchronization lock is occupied by another thread).

4.3 Other blocking: When an I/O request is made by the calling thread's sleep () or join (), the thread enters the blocking state. When the sleep () state times out, the join () waits for the thread to terminate or time out, or the I/O process completes and the thread is re-entered in a ready state.

5. Death (dead)

When a thread in the running state finishes a task or encounters another termination condition, it switches to the death state. Divided into natural termination and abnormal termination.

Third, the creation of the thread

Divided into the following types:

1. Create a thread by implementing the Runnable interface

The simplest way to create a thread is to create a class that implements the Runnable interface. By implementing the Runnable interface, we define a subtask and then pass the subtasks to thread for execution. In this way, you must use runnable as a parameter to the thread class and then create a new thread to execute the subtask by using the thread's Start method.

2. Creating threads by inheriting thread

The second way to create a thread is to create a new class that inherits the thread class and then creates an instance of the class.

The inheriting class must override the run () method, which is the entry point for the new thread. It must also call the start () method to execute.

3. Creating threads from callable and future

3.1. Create an implementation class for the callable interface and implement the call () method, which will act as the thread execution body and have a return value.

3.2. Create an instance of the callable implementation class, using the Futuretask class to wrap the callable object, which encapsulates the return value of the call () method of the Callable object.

3.3. Use the Futuretask object as the target of the thread object to create and start a new thread.

3.4. Call the Get () method of the Futuretask object to get the return value after the child thread finishes executing

Iv. Priority of Threads

Each thread has a "priority", the priority can be expressed as an integer, the value range is 0~10,0 is the lowest priority, the 10-bit highest priority, when deciding which thread needs to schedule, first to see if there is a high priority of the scheduled thread, if present, choose to dispatch from it. When the thread's time slice arrives, the system checks to see if there is another, higher-priority, scheduled thread that is scheduled if it exists. The scheduler thread is then judged in turn.

Software Construction (review)--some knowledge of 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.