Skip to content

Commit 2ddb27b

Browse files
author
kanastasov
committed
5 Thread Polls
1 parent a0e5f19 commit 2ddb27b

5 files changed

Lines changed: 98 additions & 0 deletions

File tree

5_ThreadPools/.classpath

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<classpath>
3+
<classpathentry kind="src" path="src"/>
4+
<classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER/org.eclipse.jdt.internal.debug.ui.launcher.StandardVMType/JavaSE-1.8"/>
5+
<classpathentry kind="output" path="bin"/>
6+
</classpath>

5_ThreadPools/.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
/bin/

5_ThreadPools/.project

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<projectDescription>
3+
<name>5_ThreadPools</name>
4+
<comment></comment>
5+
<projects>
6+
</projects>
7+
<buildSpec>
8+
<buildCommand>
9+
<name>org.eclipse.jdt.core.javabuilder</name>
10+
<arguments>
11+
</arguments>
12+
</buildCommand>
13+
</buildSpec>
14+
<natures>
15+
<nature>org.eclipse.jdt.core.javanature</nature>
16+
</natures>
17+
</projectDescription>
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
eclipse.preferences.version=1
2+
org.eclipse.jdt.core.compiler.codegen.inlineJsrBytecode=enabled
3+
org.eclipse.jdt.core.compiler.codegen.targetPlatform=1.8
4+
org.eclipse.jdt.core.compiler.codegen.unusedLocal=preserve
5+
org.eclipse.jdt.core.compiler.compliance=1.8
6+
org.eclipse.jdt.core.compiler.debug.lineNumber=generate
7+
org.eclipse.jdt.core.compiler.debug.localVariable=generate
8+
org.eclipse.jdt.core.compiler.debug.sourceFile=generate
9+
org.eclipse.jdt.core.compiler.problem.assertIdentifier=error
10+
org.eclipse.jdt.core.compiler.problem.enumIdentifier=error
11+
org.eclipse.jdt.core.compiler.source=1.8
Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
package threadPools;
2+
3+
/** <p>
4+
* This is the fifth tutorial on how to manage multiple threads in Java using thread pools.
5+
* With thread pools you can assign a whole collection of threads to work through your queue of tasks.
6+
* </p>
7+
*
8+
* <p>
9+
* The full tutorial and the majority of the code is available at
10+
* https://www.udemy.com/java-multithreading/?dtcode=KmfAU1g20Sjj#/
11+
* </p>
12+
*
13+
* <p>
14+
* @author kanastasov [email protected] December-2014
15+
* </p>
16+
*/
17+
18+
import java.util.concurrent.ExecutorService;
19+
import java.util.concurrent.Executors;
20+
import java.util.concurrent.TimeUnit;
21+
22+
class Processor implements Runnable {
23+
24+
private int id;
25+
26+
public Processor(int id) {
27+
this.id = id;
28+
}
29+
30+
public void run() {
31+
System.out.println("Starting: " + id);
32+
33+
try {
34+
Thread.sleep(5000);
35+
} catch (InterruptedException e) {
36+
}
37+
38+
System.out.println("Completed: " + id);
39+
}
40+
}
41+
42+
public class App {
43+
44+
public static void main(String[] args) {
45+
46+
ExecutorService executor = Executors.newFixedThreadPool(2);
47+
48+
for (int i = 0; i < 5; i++) {
49+
executor.submit(new Processor(i));
50+
}
51+
52+
executor.shutdown();
53+
54+
System.out.println("All tasks submitted.");
55+
56+
try {
57+
executor.awaitTermination(1, TimeUnit.DAYS);
58+
} catch (InterruptedException e) {
59+
}
60+
61+
System.out.println("All tasks completed.");
62+
}
63+
}

0 commit comments

Comments
 (0)