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.
Inherit Thread class: implement run method
Implement Runnable interface: implement run method
Create threads with Callable and Future
1. Compare Callable with Runnable
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 () :
The call() function returns the type passed in as V
Details you can refer to the bosses: www.cnblogs.com/jason201852…
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 — — — — — — — — — — — — — — — — — — — — — — — — — — — — — — — — — — — — — — — — — — —
(Baidu cut)
Multithreading security issues
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
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!!