当前位置:网站首页>Concurrent programming 1-2
Concurrent programming 1-2
2022-07-24 00:56:00 【wzf6667】
Understand context switching
In multithreaded programming , The number of threads is generally more than CPU Number , And every one CPU It can only be used by one thread at the same time , In order to make users feel that threads are running at the same time ,CPU The allocation of resources adopts the strategy of time slice rotation , That is to assign a time slice to each thread , Threads are occupied in time slices CPU Perform tasks , After the current thread time slice is used , It will be ready and out of the way CPU Let other threads use , This is the context switch . At the same time, the thread also needs to save the execution site of the current thread .
The timing of context switching is , The current thread is in a ready state after using the time slice , The current thread was interrupted by another thread .
thread deadlock
What is thread deadlock
Thread deadlock refers to , Two or more threads are in execution , The phenomenon of waiting for each other due to competition for resources . Such as Threads A hold 2 Resources for 1 resources , Threads B hold 1 Resource but wait 2 resources . Threads will wait for each other and cannot continue .
Four conditions for thread deadlock
5.1 mutual exclusion : The process requires that the allocated resources ( Such as a printer ) Conduct exclusive control , In other words, a resource is occupied by only one process for a period of time . At this point, if other processes request the resource , The request process can only wait .
5.2 The condition of indivisibility : The resources obtained by the process are not used up , It can't be taken away by other processes , That is to say, it can only be released by the process that obtains the resource itself ( Only active release ).
5.3 Request and hold conditions : The process has maintained at least one resource , But new resource requests were made , The resource is already occupied by other processes , The request process is blocked , But keep up with what you've got .
5.4 Loop wait condition : There is a circular waiting chain for process resources , The resources obtained by each process in the chain are Requested by the next process in the chain . That is, there is a collection of processes waiting {Pl, P2, …, pn}, among Pi etc. Waiting resources are P(i+1) occupy (i=0, 1,
…, n-1),Pn Waiting resources are P0 occupy , Pictured 2-15 Shown .Intuitively , The loop wait condition seems to be the same as the definition of deadlock , It's not . According to the definition of deadlock, a waiting ring is formed
More stringent conditions are required , It requires Pi The waiting resource must be P(i+1) To satisfy , However, there is no such restriction for the loop waiting condition .
for example , There are two output devices in the system ,P0 Take possession of one ,PK Take possession of another one , And K It doesn't belong to a collection {0, 1, …, n}.Pn Waiting for an output device , It can come from P0 get , Maybe from PK get . therefore , although Pn、P0 And others
Some processes form a circular waiting circle , but PK Not in the circle , if PK Release the output device , You can break the cycle of waiting . So loop waiting is only a necessary condition for deadlock .
————————————————
Copyright notice : This paper is about CSDN Blogger 「wljliujuan」 The original article of , follow CC 4.0
BY-SA Copyright agreement , For reprint, please attach the original source link and this statement .
Link to the original text :https://blog.csdn.net/wljliujuan/article/details/79614019
Examples of deadlocks
private static volatile Object A = new Object();
private static volatile Object B = new Object();
public static void main(String[] args) throws InterruptedException {
Thread threadA = new Thread(new Runnable() {
@Override
public void run() {
synchronized (A){
System.out.println(Thread.currentThread()+"got A");
// try {
// Thread.sleep(10000);
// } catch (InterruptedException e) {
// e.printStackTrace();
// }
System.out.println(Thread.currentThread()+"waiting B");
synchronized (B){
System.out.println(Thread.currentThread()+"got B");
}
}
}
});
Thread threadB = new Thread(new Runnable() {
@Override
public void run() {
synchronized (B){
System.out.println(Thread.currentThread()+"got B");
// try {
// Thread.sleep(10000);
// } catch (InterruptedException e) {
// e.printStackTrace();
// }
System.out.println(Thread.currentThread()+"waiting A");
synchronized (A){
System.out.println(Thread.currentThread()+"got A");
}
}
}
});
threadA.start();
threadB.start();
}
Thread[Thread-1,5,main]got B
Thread[Thread-0,5,main]got A
Thread[Thread-1,5,main]waiting A
Thread[Thread-0,5,main]waiting B
边栏推荐
- Starfish OS: create a new paradigm of the meta universe with reality as the link
- Tutorial on the principle and application of database system (046) -- MySQL query (VIII): group by
- 测试小码农也有大目标,最新BAT大厂面试题大总结(持续更新中...)
- Educational Codeforces Round 132 (Rated for Div. 2) D. Rorororobot
- Summary of polynomial commitment schemes
- SAP 电商云 Spartacus UI Store 相关的设计明细
- scroll-view實現下拉刷新(避免onload進入頁面初始refresher-triggered為true觸發下拉問題)
- Classic example of C language - find the minimum number of banknotes
- Network system experiment: solve the problem of Ping failure
- EFCore高级Saas系统下单DbContext如何支持不同数据库的迁移
猜你喜欢

How to relieve the pillow quickly

Development of main applet for business card traffic near the map

Fpga:ov7725 camera displays images in rgb565 format through vga/hdmi

The way to access global variables in multi-source file mode (extern usage)

Classic examples of C language - adding two scores

Starfish OS: create a new paradigm of the meta universe with reality as the link

網絡系統實驗:ping不通的問題解决

Intelligent video monitoring solutions for elderly care institutions, using new technologies to help the intelligent supervision of nursing homes

Sparksql design and introduction, 220722,

Printf function - conversion description
随机推荐
Selection method of geometric objects in Creo 9.0
An article teaches you the basic use of kubernetes
MariaDB database upgrade version
Development of main applet for business card traffic near the map
Tutorial on the principle and application of database system (044) -- MySQL query (VI): using the limit option to realize paging query
Database connection pool & dbutils
Dynamic rip configuration
如何在自动化测试中使用MitmProxy获取数据返回?
采坑websocket總結
这是一道大水题
Tutorial on the principle and application of database system (046) -- MySQL query (VIII): group by
[flyway introduction]
Fpga:ov7725 camera displays images in rgb565 format through vga/hdmi
JS related knowledge
VIM common commands
C language writing specification
vim常用命令
Testers who have been employed for 3 months are facing employment confirmation. Leaders: 1 year of work experience is packaged into 5 years, and the probation period is eliminated
Classic examples of C language - use 4 × The matrix displays all integers from 1 to 16 and calculates the sum of each row, column, and diagonal
scroll-view实现下拉刷新(避免onload进入页面初始refresher-triggered为true触发下拉问题)