Skip to content

Commit db6c5aa

Browse files
committed
Deadlock & Starvation
1 parent 9bcc3cc commit db6c5aa

File tree

18 files changed

+335
-0
lines changed

18 files changed

+335
-0
lines changed

Concurrency/007_deadlock_Part2/.idea/misc.xml

Lines changed: 6 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Concurrency/007_deadlock_Part2/.idea/modules.xml

Lines changed: 8 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Concurrency/007_deadlock_Part2/.idea/workspace.xml

Lines changed: 86 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<module type="JAVA_MODULE" version="4">
3+
<component name="NewModuleRootManager" inherit-compiler-output="true">
4+
<exclude-output />
5+
<content url="file://$MODULE_DIR$">
6+
<sourceFolder url="file://$MODULE_DIR$/src" isTestSource="false" />
7+
</content>
8+
<orderEntry type="inheritedJdk" />
9+
<orderEntry type="sourceFolder" forTests="false" />
10+
</component>
11+
</module>
696 Bytes
Binary file not shown.
696 Bytes
Binary file not shown.
1.03 KB
Binary file not shown.
819 Bytes
Binary file not shown.
Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
public class Main {
2+
3+
public static void main(String[] args) {
4+
final PolitePerson jane = new PolitePerson("Jane");
5+
final PolitePerson john = new PolitePerson("John");
6+
7+
new Thread(new Runnable() {
8+
@Override
9+
public void run() {
10+
jane.sayHello(john);
11+
}
12+
}).start();
13+
14+
new Thread(new Runnable() {
15+
@Override
16+
public void run() {
17+
john.sayHello(jane);
18+
}
19+
}).start();
20+
}
21+
22+
// 1. Thread1 acquires the lock on the jane object and enters the sayHello() method.
23+
// It prints to the console, then suspends.
24+
// 2. Thread2 acquires the lock on the john object and enters the sayHello() method.
25+
// It prints to the console, then suspends.
26+
// 3. Thread1 runs again and wants to say hello back to the john object. It tries to call the sayHelloBack() method
27+
// using the john object that was passed into the sayHello() method,
28+
// but Thread2 is holding the john lock, so Thread1 suspends.
29+
// 4. Thread2 runs again and wants to say hello back to the jane object. It tries to call the sayHelloBack() method
30+
// using the jane object that was passed into the sayHello() method,
31+
// but Thread1 is holding the jane lock, so Thread2 suspends.
32+
33+
static class PolitePerson {
34+
private final String name;
35+
36+
public PolitePerson(String name) {
37+
this.name = name;
38+
}
39+
40+
public String getName() {
41+
return name;
42+
}
43+
44+
public synchronized void sayHello(PolitePerson person) {
45+
System.out.format("%s: %s" + " has said hello to me!%n", this.name, person.getName());
46+
person.sayHelloBack(this);
47+
}
48+
49+
public synchronized void sayHelloBack(PolitePerson person) {
50+
System.out.format("%s: %s" + " has said hello back to me!%n", this.name, person.getName());
51+
}
52+
}
53+
}

Concurrency/008_Starvation/.idea/misc.xml

Lines changed: 6 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)