First let’s talk about process, what is process!!

Process: refers to a running application (App on a mobile phone, program on a PC). It is the basic unit of resource allocation and scheduling. A program or application has at least one process.

Threads: Threads are units of execution with minimal CPU scheduling (smaller than processes) that can run independently and are limited system resources.

** The relationship between the two processes ** : a process contains at least one or more threads, (contained relationship) processes have a separate running space, the threads share this space, also own all the resources of the process,, so can execute concurrently (parallel).

** Three ways to create threads **

Inherit Thread class: implement run method

Implement Runnable interface: implement run method

Create threads with Callable and Future

1. Compare Callable with Runnable
Because the run() method returns a void value, no results can be returned after the task is executed.

Callable, located under the java.util.concurrent package, is also an interface in which only one method is declared, but this method is called Call () :

This is a generic interface,

The call() function returns the type passed in as V

Details you can refer to the bosses: www.cnblogs.com/jason201852…

** Thread lifecycle ** : New, Ready, Run, block, Destroy (cut one somewhere)

Threads can also block

I’ve got three here

(01) Wait to block — A thread is told to wait for work to complete by calling its wait() method.

(02) Synchronized blocking — A thread that fails to acquire a synchronized lock (because the lock is occupied by another thread) enters a synchronized blocking state.

(03) Other blocking — a thread enters a blocking state by calling its sleep() or join() or by issuing an I/O request. When the sleep() state times out, when the join() wait thread terminates or times out, or when I/O processing is complete, the thread goes back to the ready state.

— — — — — — — — — — — — — — — — — — — — — — — — — — — — — — — — — — — — — — — — — — — — line — — — — — — — — — — — — — — — — — — — — — — — — — — — — — — — — — — — — — — — — — — —

** Multiple processes ** : Specify attributes for the four components in the manifest file android:process Enable multiple processes (memory allows to enable N processes)
** Concurrent execution ** : Multiple tasks are executed one at a time, so that nothing can be seen because they run so fast, but only one program is running at a time.
** Parallel execution ** : Multiple tasks are executed simultaneously, in no particular order

(Baidu cut)

Multithreading security issues

** Why is thread unsafe ** : Multiple threads are accessing a variable at the same time and modifying it at the same time.
Only one thread can access the current resource at a time. Synchronized, lock, and ReadWriteLock are synchronized, synchronized, and ReadWriteLock.

I’ll start with A simple example: let’s say I have data int I = 100, thread A and thread B. Thread A calls I first and adds 10 to it. Before the callback, thread B also requests I and decreases by 20. After the reduction, thread A has completed the callback and I’s data becomes 110. Thread B’s request for I is 100, and then it returns 80. Thread B’s request for I is 100, and then it returns 80. This is what I understand as thread safety

How to solve the thread safety problem

** Lock ** : can be added to methods, can be added to code blocks. Methods can be added to static methods and instance methods. Code blocks can be added in two ways: this and class names. Class two.

The detailed method of: blog.csdn.net/zxq125521/a…

Forgive me for not wanting to write!! So I got you a big guy’s code, and I’m sure you’re smart enough not to care about the details

Then there’s a thread pool

Introduction to thread pools: A thread pool is a collection of threads that are first created, called a thread pool. Using a thread pool is a good way to improve performance, the thread pool at system startup is to create a large number of idle threads, program will be a task to the thread pool, the thread pool will start a thread to execute this task, after the execution, the thread will not die, but again returned to the thread pool become idle, waiting for the next mission. A thread can only perform one task at a time, but can submit multiple tasks to a thread pool at the same time. Multi-thread running time, the system constantly start and close the new thread, the cost is very high, will transition consumption of system resources, as well as transition switch thread danger, which may lead to the collapse of system resources. In this case, thread pools are the best choice

Well, the end of this blog, you see here add a follow ah

What question can leave a message in the comment area (request private letter) oh, the programming road is long, we share together!!