当前位置:网站首页>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
边栏推荐
- memcached安装
- 深入学习JVM底层(四):类文件结构
- Let every developer use machine learning technology
- Go 学习笔记整合
- Comment utiliser mitmproxy
- ZABBIX server trap command injection vulnerability (cve-2017-2824)
- Keepalived installation, use and quick start
- Linkage between esp8266 and stc8h8k single chip microcomputer - Weather Clock
- 谷歌出海创业加速器报名倒计时 3 天,创业人闯关指南提前收藏!
- Replace Django database with MySQL (attributeerror: 'STR' object has no attribute 'decode')
猜你喜欢
CNN visualization technology -- detailed explanation of cam & grad cam and concise implementation of pytorch
Data playback partner rviz+plotjuggler
官方零基础入门 Jetpack Compose 的中文课程来啦!
Little bear sect manual query and ADC in-depth study
浏览器原理思维导图
Memcached installation
Step by step | help you easily submit Google play data security form
脑与认知神经科学Matlab Psytoolbox认知科学实验设计——实验设计四
ROS create workspace
深入了解JUC并发(一)什么是JUC
随机推荐
深入了解JUC并发(一)什么是JUC
Eco express micro engine system has supported one click deployment to cloud hosting
Flutter 混合开发: 开发一个简单的快速启动框架 | 开发者说·DTalk
51单片机——ADC讲解(A/D转换、D/A转换)
External interrupts cannot be accessed. Just delete the code and restore it Record this unexpected bug
从设计交付到开发,轻松畅快高效率!
Zabbix Server trapper 命令注入漏洞 (CVE-2017-2824)
Format check JS
社区说|Kotlin Flow 的原理与设计哲学
使用HBuilderX的一些常用功能
AttributeError: ‘str‘ object has no attribute ‘decode‘
Redis key value database [advanced]
LeetCode 40. 组合总和 II
LeetCode 27. Removing Elements
Redis Key-Value数据库【初级】
LeetCode 77. combination
Invalid operation: Load into table ‘sources_orderdata‘ failed. Check ‘stl_load_errors‘ system table
Detailed notes of ES6
Spark overview
Frequently asked questions about jetpack compose and material you