Java multithreading.

This is the back-end small class of the monastery. Each article is shared from

[background introduction] [knowledge analysis] [common problems] [solutions] [coding practice] [extended thinking] [more discussion] [References]

Eight aspects of in-depth analysis of back-end knowledge / skills. This article shares:

[Java multithreading]

Hello, I'm a student of the tenth phase of Zhengzhou branch of it Academy. I'm an honest, pure and kind java programmer

Today, I'd like to share with you the knowledge points in deep thinking about Java task 10 on the official website of Xiuzhen Academy -- a preliminary exploration of Java multithreading.

Background introduction

Multithreading is a very important knowledge point of Java. To understand multithreading, we should first understand two basic concepts, process and thread.

Process: a running activity of a program with certain independent functions about a data set. Simple understanding is the execution process of a program.

In short, the concept of process mainly has two points: first, process is an entity. Each process has its own address space. Generally, It includes text region, data region and stack region. The text region stores the code executed by the processor; the data region stores variables and dynamically allocated memory used during process execution; the stack region stores the instructions and local variables called by the active procedure. Second, the process is an "executing program" 。 A program is an inanimate entity. It can become an active entity only when the processor gives the program life. We call it a process.

Thread: each task executed in a single process is a thread.

Threads do not have a separate address space, but have their own stack segments to store local and temporary variables. All threads of a process can share all resources of the process.

Process is the smallest unit that the operating system allocates resources, and thread is the smallest unit that the operating system schedules.

Compared with the process, the overhead of thread is small, the switching speed is fast, and the address space is shared. It is convenient to communicate. In order to improve the system efficiency, multithreading can be used.

Knowledge analysis

New status:

After a thread object is created by using the new keyword and the thread class or its subclasses, the thread object is in the new state. It remains in this state until the program starts () the thread.

Ready status:

When the thread object calls the start () method, the thread enters the ready state. The thread in the ready state is in the ready queue and needs to wait for the scheduling of the thread scheduler in the JVM.

Operation status:

If the ready thread gets CPU resources, it can execute run (), and the thread is running. Threads in the running state are the most complex, and can become blocked, ready, and dead.

Blocking status:

If a thread executes sleep, suspend and other methods, and loses its occupied resources, the thread will enter the blocking state from the running state. After the sleep time has expired or the device resources have been obtained, it can re-enter the ready state. It can be divided into three types:

Wait blocking: the thread in the running state executes the wait () method to make the thread enter the wait blocking state.

Synchronization blocking: the thread failed to acquire the synchronized synchronization lock (because the synchronization lock is occupied by other threads).

Other blocking: when an I / O request is issued by calling the thread's sleep () or join (), the thread will enter the blocking state. When the sleep () state times out, the join () waits for the thread to terminate or time out, or the I / O processing is completed, and the thread returns to the ready state.

Death status:

When a running thread completes a task or other termination conditions occur, the thread switches to the termination state.

common problem

There are three ways to implement multithreading in Java.

Extended thinking

Thread scheduling method

thread priority

The setpriority () and getpriority () methods of the thread class are used to set and obtain the priority of the thread respectively.

Thread sleep: thread Sleep (long millis) method to make the thread go to the blocking state. The millis parameter sets the sleep time in milliseconds. When sleep is over, it turns to ready state.

Thread: thread The yield () method pauses the thread object currently executing and gives the execution opportunity to threads with the same or higher priority.

Yield () actually just returns the currently running thread to the ready state to allow other threads with the same priority to get the chance to run. Therefore, the purpose of using yield () is to make threads with the same priority rotate appropriately. However, in practice, yield () cannot be guaranteed to achieve the purpose of concession, because the concession thread may also be selected again by the thread scheduler.

Thread join: join () method, waiting for other threads to terminate. When the join () method of another thread is invoked in the current thread, the current thread is transferred to the blocking state until the end of the other process is completed, and the current thread is then changed from blocking to ready state.

Thread wait: the wait() method in the object class causes the current thread to wait until other threads call the notify() method or notifyall() method of the object to wake up.

Thread wakeup: the notify () method in the object class wakes up a single thread waiting on this object monitor. If all threads are waiting on this object, one of them will be selected to wake up. The choice is arbitrary and occurs when a decision is made about the implementation. The thread waits on the object's monitor by calling one of the wait methods. The awakened thread cannot continue executing until the current thread relinquishes the lock on this object. The awakened thread will compete with all other threads actively synchronized on the object in a conventional manner; A similar method is notifyall(), which wakes up all threads waiting on this object monitor.

Interrupt(): only throws an interrupt exception, but if the exception is caught, the thread will not be interrupted directly.

reference

More discussion

Q1: the difference between the start () method and the run () method

A1: only when the start () method is called can it show the characteristics of multithreading. The code in the run () method of different threads is executed alternately. If you only call the run () method, the code is executed synchronously. You must wait until all the code in one thread's run () method is executed before another thread can execute the code in its run () method.

Q2: difference between runnable interface and callable interface

A2: the return value of the run () method in the runnable interface is void. What it does is simply to execute the code in the run () method; The call () method in the callable interface has a return value and is a generic type. It can be used to obtain the results of asynchronous execution in combination with future and futuretask.

Q3: what is multithreaded context switching

A3: context switching of multithreading refers to the process of switching the CPU control right from one running thread to another thread ready and waiting to obtain the CPU execution right.

Ppt link video link

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>