Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 9bcc3cc

Browse files
committed
Thread Pools & DeadLock
1 parent 6277ce1 commit 9bcc3cc

File tree

19 files changed

+428
-0
lines changed

19 files changed

+428
-0
lines changed

‎Concurrency/005_Thread-Pools/.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/005_Thread-Pools/.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/005_Thread-Pools/.idea/workspace.xml‎

Lines changed: 88 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>
1 KB
Binary file not shown.
2.27 KB
Binary file not shown.
1.96 KB
Binary file not shown.
2.21 KB
Binary file not shown.
676 Bytes
Binary file not shown.
Lines changed: 132 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,132 @@
1+
package com.ashlesh;
2+
3+
import java.util.Random;
4+
import java.util.concurrent.*;
5+
6+
import static com.ashlesh.Main.EOF;
7+
8+
public class Main {
9+
public static final String EOF = "EOF";
10+
11+
public static void main(String[] args) {
12+
ArrayBlockingQueue<String> buffer = new ArrayBlockingQueue<String>(6);
13+
14+
ExecutorService executorService = Executors.newFixedThreadPool(5);
15+
16+
MyProducer producer = new MyProducer(buffer, ThreadColor.ANSI_YELLOW);
17+
MyConsumer consumer1 = new MyConsumer(buffer, ThreadColor.ANSI_PURPLE);
18+
MyConsumer consumer2 = new MyConsumer(buffer, ThreadColor.ANSI_CYAN);
19+
20+
executorService.execute(producer);
21+
executorService.execute(consumer1);
22+
executorService.execute(consumer2);
23+
24+
Future<String> future = executorService.submit(new Callable<String>() {
25+
@Override
26+
public String call() throws Exception {
27+
System.out.println(ThreadColor.ANSI_WHITE + "I'm being printed for the Callable class");
28+
return "This is the callable result";
29+
}
30+
});
31+
32+
try {
33+
System.out.println(future.get());
34+
} catch(ExecutionException e) {
35+
System.out.println("Something went wrong");
36+
} catch(InterruptedException e) {
37+
System.out.println("Thread running the task was interrupted");
38+
}
39+
40+
executorService.shutdown();
41+
}
42+
}
43+
44+
class MyProducer implements Runnable {
45+
private ArrayBlockingQueue<String> buffer;
46+
private String color;
47+
48+
public MyProducer(ArrayBlockingQueue<String> buffer, String color) {
49+
this.buffer = buffer;
50+
this.color = color;
51+
}
52+
53+
public void run() {
54+
Random random = new Random();
55+
String[] nums = { "1", "2", "3", "4", "5"};
56+
57+
for(String num: nums) {
58+
try {
59+
System.out.println(color + "Adding..." + num);
60+
buffer.put(num);
61+
62+
Thread.sleep(random.nextInt(1000));
63+
} catch(InterruptedException e) {
64+
System.out.println("Producer was interrupted");
65+
}
66+
}
67+
68+
System.out.println(color + "Adding EOF and exiting...");
69+
try {
70+
buffer.put("EOF");
71+
} catch(InterruptedException e) {
72+
}
73+
}
74+
}
75+
76+
class MyConsumer implements Runnable {
77+
private ArrayBlockingQueue<String> buffer;
78+
private String color;
79+
80+
public MyConsumer(ArrayBlockingQueue<String> buffer, String color) {
81+
this.buffer = buffer;
82+
this.color = color;
83+
}
84+
85+
public void run() {
86+
87+
while(true) {
88+
synchronized (buffer) {
89+
try {
90+
if (buffer.isEmpty()) {
91+
continue;
92+
}
93+
94+
if (buffer.peek().equals(EOF)) {
95+
System.out.println(color + "Exiting");
96+
break;
97+
} else {
98+
System.out.println(color + "Removed " + buffer.take());
99+
}
100+
} catch (InterruptedException e) {
101+
102+
}
103+
}
104+
}
105+
}
106+
}
107+
108+
109+
110+
111+
112+
113+
114+
115+
116+
117+
118+
119+
120+
121+
122+
123+
124+
125+
126+
127+
128+
129+
130+
131+
132+

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /