当前位置:网站首页>In depth understanding of JUC concurrency (I) what is JUC
In depth understanding of JUC concurrency (I) what is JUC
2022-07-02 06:14:00 【I think starfish_ ninety-eight】
List of articles
Basic knowledge of
What is? JUC
JUC Namely java.util.concurrent The following class package , Dedicated to multithreading development .
The advantages and disadvantages of concurrent programming
advantage
Make full use of multicore CPU Computing power
Multicore CPU In the background of , The trend of concurrent programming , Through the form of concurrent programming, multi-core can be CPU To the extreme , Improved performance .
It is convenient for business splitting , Improve the concurrent ability and performance of the system
The current system requires millions or even tens of millions of levels of concurrency , And multithreading concurrent programming is the foundation of developing high concurrent system , Using many thread mechanisms can greatly improve the overall concurrency and performance of the system .
shortcoming
Frequent context switching
The task is a context switch from saving to reloading , And every time you switch , Need to save the current status , In order to restore the previous state , And this switch is very lossy , Too often it can't take advantage of multithreaded programming
Thread safety problem
The most difficult problem in multithreading programming is thread safety in critical area , If you don't pay attention to it a little bit, you will get deadlock , Once a deadlock occurs, it will make the system function unavailable
Blocking and non blocking
The focus of blocking and non blocking is The behavior of a thread waiting for a message , While waiting for the news , The current thread is suspended , Or not suspended .
- Blocking : Call after sending out , Before the message returns , The current thread will be suspended , Until a message returns , The current thread will be activated
- Non blocking : Call after sending out , Does not block the current thread , And will return immediately
Synchronous and asynchronous
- Sync : When a synchronous call is sent , caller Always wait for the call result to return after , To carry out subsequent operations
- asynchronous : When an asynchronous call is sent , caller Regardless of whether the called method is completed , Will continue to execute the following code . Asynchronous call , To get results , There are generally two ways :
- Results of active polling asynchronous calls
- Callee passed callback To notify the caller of the result of the call
Concurrency and parallelism
Concurrent
Threads take turns using cpu This is called concurrency

parallel
Multiple cpu Next , Each core can schedule running threads , At this time, the thread can be parallel
Processes and threads
process
A process can be considered an instance of a program . Most programs can run multiple instance processes at the same time ( For example, Notepad 、 drawing 、 The browser etc. ), Some programs can only start one instance process ( For example, Netease cloud music 、360 Safety guard ) etc.
Threads
- A process can be divided into one or more threads . A thread is a stream of instructions , Give instructions in a certain order to CPU perform
- Java in , Thread as the minimum scheduling unit , Process is the smallest unit of resource allocation , stay windows The middle process is inactive , Just as a container for threads
State of thread
public enum State {
// function
NEW,
// function
RUNNABLE,
// Blocking
BLOCKED,
// wait for
WAITING,
// Overtime waiting
TIMED_WAITING,
// End
TERMINATED;
}
- After the thread is created, it will be in the NEW( newly build ) state
- call start() Start running after method , And get the CPU Time slice (timeslice) After that RUNNING( function ) state
- When a thread calls Synchronization method when , Without obtaining the lock , The thread will enter the BLOCKED( Blocking ) state
- When the thread executes wait() After method , Thread entry WAITING( wait for ) state
- stay Wait state On the basis of the increase of the timeout limit , Such as through sleep(long millis) Method or wait(long millis) Method can put the thread into waiting timeout (TIMED WAITING) state
- After the thread runs, it will enter TERMINATED( End ) state
边栏推荐
猜你喜欢
随机推荐
MUI底部导航的样式修改
Stc8h8k series assembly and C51 actual combat - keys allow key counting (using falling edge interrupt control)
经典文献阅读之--SuMa++
uni-app开发中遇到的问题(持续更新)
Detailed explanation of BGP message
LeetCode 27. 移除元素
sudo提权
Replace Django database with MySQL (attributeerror: 'STR' object has no attribute 'decode')
神机百炼3.53-Kruskal
BGP报文详细解释
Redis key value database [advanced]
Talking about MySQL database
外部中断无法进入,删代码再还原就好......记录这个想不到的bug
keepalived安装使用与快速入门
Verifying downloaded files using sha256 files
Format check JS
Community theory | kotlin flow's principle and design philosophy
Memcached installation
Deep learning classification network -- vggnet
LeetCode 47. Full arrangement II

![[C language] simple implementation of mine sweeping game](/img/f7/15d561b3c329847971cabd4708c851.png)







