当前位置:网站首页>What is JUC in high concurrency programming
What is JUC in high concurrency programming
2022-06-12 15:41:00 【It mapper】
JUC What is high concurrency programming JUC
JUC brief introduction
- stay Java in , The thread part is a focus , JUC It's also about threads .JUC Namely java.util .concurrent It's short for toolkit . This is a toolkit for processing threads ,JDK 1.5 The beginning of
![[ Failed to transfer the external chain picture , The origin station may have anti-theft chain mechanism , It is suggested to save the pictures and upload them directly (img-Cm9nizU6-1646008008803)(C:\Users\admin\AppData\Roaming\Typora\typora-user-images\1646007408924.png)]](/img/fd/eb28d24fdab4a640354196653d87b2.jpg)
Processes and threads
process (Process) A program in a computer is about a running activity on a data set , Is the system resource allocation and scheduling of the basic unit , Is the foundation of the operating system architecture . In modern thread oriented computer architecture , A process is a container for threads . A program is an instruction 、 Of data and its organization describe , A process is an entity of a program . A program in a computer is about a running activity on a data set , Is the system resource allocation and scheduling of the basic unit , Is the foundation of the operating system architecture . A program is an instruction 、 Description of data and its organization , A process is an entity of a program
Threads (thread) It is the smallest unit that the operating system can schedule operations . It is included in the process , Is the actual operation unit in the process . A thread refers to a single sequential control flow in a process , Multiple threads can be concurrent in one process , Each thread performs different tasks in parallel
![[ Failed to transfer the external chain picture , The origin station may have anti-theft chain mechanism , It is suggested to save the pictures and upload them directly (img-9swGhnUU-1646008008805)(C:\Users\admin\AppData\Roaming\Typora\typora-user-images\1646007482119.png)]](/img/a8/933aeb89c2a4c7582b32052af9591e.jpg)
process : An application running in a system ; Once the program runs, it's a process ; process —— The smallest unit of resource allocation
Threads : The basic unit of time resources allocated to the processor by the system , Or a unit execution flow executed independently within a process . Threads —— The smallest unit of program execution
State of thread
public enum State { /** * Thread state for a thread which has not yet started. */ NEW,( newly build ) /** * Thread state for a runnable thread. A thread in the runnable * state is executing in the Java virtual machine but it may * be waiting for other resources from the operating system * such as processor. */ RUNNABLE,( Be ready ) /** * Thread state for a thread blocked waiting for a monitor lock. * A thread in the blocked state is waiting for a monitor lock * to enter a synchronized block/method or * reenter a synchronized block/method after calling * {@link Object#wait() Object.wait}. */ BLOCKED,( Blocking ) /** * Thread state for a waiting thread. * A thread is in the waiting state due to calling one of the * following methods: * <ul> * <li>{@link Object#wait() Object.wait} with no timeout</li> * <li>{@link #join() Thread.join} with no timeout</li> * <li>{@link LockSupport#park() LockSupport.park}</li> * </ul> ** <p>A thread in the waiting state is waiting for another thread to * perform a particular action. * * For example, a thread that has called <tt>Object.wait()</tt> * on an object is waiting for another thread to call * <tt>Object.notify()</tt> or <tt>Object.notifyAll()</tt> on * that object. A thread that has called <tt>Thread.join()</tt> * is waiting for a specified thread to terminate. */ WAITING,( Be there or be square ) /** * Thread state for a waiting thread with a specified waiting time. * A thread is in the timed waiting state due to calling one of * the following methods with a specified positive waiting time: * <ul> * <li>{@link #sleep Thread.sleep}</li> * <li>{@link Object#wait(long) Object.wait} with timeout</li> * <li>{@link #join(long) Thread.join} with timeout</li> * <li>{@link LockSupport#parkNanos LockSupport.parkNanos}</li> * <li>{@link LockSupport#parkUntil LockSupport.parkUntil}</li> * </ul> */ TIMED_WAITING,( Out of date ) /** * Thread state for a terminated thread. * The thread has completed execution. */ TERMINATED;( End ) }
Concurrency and parallelism
Serial mode
- Serial means that all tasks are carried out one by one in order . Serial means you have to load a load of firewood before you can deliver it , Only delivery to , To get rid of the firewood , And only completed the whole three steps , To move on to the next step , Serial is only one task at a time , And perform the task
Parallel mode
- Parallel means that you can get multiple tasks at the same time , And at the same time to carry out these tasks . Parallel mode is equivalent to a long queue , Divided into short queues , So parallelism shortens the length of the task queue . The efficiency of parallelism is strongly dependent on multiprocesses at the code level / Multithreaded code , From the hardware point of view, it depends on multi-core CPU
Concurrent
- Concurrent (concurrent) It refers to the phenomenon that multiple programs can run at the same time , More detailed is that multiple processes can run at the same time or multiple instructions can run at the same time . But that's not the point , When describing concurrency, we will not deduct the accuracy of such words , The point of concurrency is that it's a phenomenon , Concurrent description Is the phenomenon of multiple processes running at the same time . But actually , For a single core CPU Come on , At the same time Only one thread can run . therefore , there " Running at the same time " It doesn't mean that there are multiple at the same time Thread running phenomenon , This is the concept of parallelism , Instead, it provides a function for users to see multiple programs running at the same time , But in fact, the process of these programs is not always occupied CPU Of , It's a little execution, a little pause
- To solve the problem of large concurrency , It is usually to break down a large task into several small tasks , Because the process scheduling of the operating system is random , So after splitting into several small tasks , May be performed from any small task . There may be some phenomena
- A small task may have been executed more than once , I haven't started the next task yet . At this time, queues or similar data structures will be used to store the results of various small tasks
- There may be the possibility of performing the second step before the first step is ready . At this time , It's usually multiplex or asynchronous , For example, only when you are ready to generate an event notification can you perform a task
- Can be multi process / Multithreading to perform these small tasks in parallel . Or single process / A single thread performs these small tasks , At this time, it is likely to cooperate with multiplexing to achieve high efficiency
Tube side
- Tube side (monitor) Is to ensure that only one process is active in the process at the same time , That is, the operation defined in the process is called by only one process at the same time ( Implemented by compiler ). However, this does not guarantee that the process will execute in the designed order JVM Synchronization is based on entering and exiting the tube (monitor) Object implemented , Each object has a tube side (monitor) object , Tube side (monitor) Will follow java Objects are created and destroyed together The execution thread must first hold the pipe object , Then we can execute the method , When the method is completed, the tube side will be released , The method will hold the pipeline when executing , Other threads can no longer get the same process
User threads and daemons
User threads : Ordinary threads used at ordinary times , Custom thread
The guardian thread : Running in the background , Is a special thread , For example, garbage collection
When the main thread ends , The user thread is still running ,JVM Survive
If there is no user thread , It's all daemon threads ,JVM end
边栏推荐
- 5G新方案!升级现有的基站和UE模拟器至5G毫米波频段
- IGMP message (tcp/ip details volume 1/ Volume 2)
- xshell 7 官网免费下载
- Introduction to microservices
- Error 1105: message:\“raft entry is too large
- Seaborn的简述
- Great God cracked the AMD k6-2+ processor 22 years ago and opened the hidden 128KB L2 cache
- Understanding of Odom coordinate system
- Idea pull branch code
- Classic case of solidity - Smart games
猜你喜欢

应势而变,2022年下半场的升级之路

Solving multithreading security problems

Classic case of solidity - Smart games

Servlet知识详解(2)

Broadcast and multicast (tcp/ip details volume 1/2)

Defer learning in golang

Increase the maximum number of MySQL connections

Acwing summer daily question (sexy prime number on June 10)

redis String类型常见命令

RARP总结(TCP/IP详解卷1/2)
随机推荐
Codeworks round 797 (Div. 3, cf1690)
Broadcast and multicast (tcp/ip details volume 1/2)
Pta: self test -2 prime pair conjecture (20 points)
Two ways of array simulating queue
Seaborn Brief
Instructions de soumission des tâches télécharger les tâches sur le disque réseau
FIRSTVT and LASTVT vernacular
The difference between TCP and UDP, the three handshakes of TCP and the four waves of TCP
TS 22.011
Module yaml error: Unexpected key in data: static_ context [line 9 col 3]
C language partition bin file program
[jvm learning] virtual machine stack
Idea大全(转载)
Acwing summer daily question (sexy prime number on June 10)
IGMP message (tcp/ip details volume 1/ Volume 2)
Classification of annotations
Servlet knowledge explanation (2)
Solve log4j2 vulnerability and be attacked by mining and zombie process viruses
[untitled]
SOA Architecture