Skip to content

Commit 53896fc

Browse files
committed
Trying source code example of java8-concurrency-tutorial by winterbe. Part 1: Threads and Executors
1 parent 31e86d9 commit 53896fc

20 files changed

+520
-0
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ Learning what's new in Java SE 8. My learning path includes content from java tu
2020
* [Java Platform Standard Edition 8 Documentation](http://docs.oracle.com/javase/8/docs/)
2121
* [Java™ Platform, Standard Edition 8 API Specification](http://docs.oracle.com/javase/8/docs/api/)
2222
* [Java 8 Lambdas - A Peek Under the Hood](http://www.infoq.com/articles/Java-8-Lambdas-A-Peek-Under-the-Hood)
23+
* [Java 8 Tutorial by Benjamin Winterberg](http://winterbe.com/posts/2014/03/16/java-8-tutorial/)
2324

2425
### Presentations
2526
* [Java 8 in Anger](http://www.infoq.com/presentations/java8-examples)
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<classpath>
3+
<classpathentry kind="src" output="target/classes" path="src/main/java">
4+
<attributes>
5+
<attribute name="optional" value="true"/>
6+
<attribute name="maven.pomderived" value="true"/>
7+
</attributes>
8+
</classpathentry>
9+
<classpathentry kind="src" output="target/test-classes" path="src/test/java">
10+
<attributes>
11+
<attribute name="optional" value="true"/>
12+
<attribute name="maven.pomderived" value="true"/>
13+
</attributes>
14+
</classpathentry>
15+
<classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER/org.eclipse.jdt.internal.debug.ui.launcher.StandardVMType/J2SE-1.5">
16+
<attributes>
17+
<attribute name="maven.pomderived" value="true"/>
18+
</attributes>
19+
</classpathentry>
20+
<classpathentry kind="con" path="org.eclipse.m2e.MAVEN2_CLASSPATH_CONTAINER">
21+
<attributes>
22+
<attribute name="maven.pomderived" value="true"/>
23+
</attributes>
24+
</classpathentry>
25+
<classpathentry kind="output" path="target/classes"/>
26+
</classpath>
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
/target/
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<projectDescription>
3+
<name>java8-concurrency</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+
<buildCommand>
14+
<name>org.eclipse.m2e.core.maven2Builder</name>
15+
<arguments>
16+
</arguments>
17+
</buildCommand>
18+
</buildSpec>
19+
<natures>
20+
<nature>org.eclipse.jdt.core.javanature</nature>
21+
<nature>org.eclipse.m2e.core.maven2Nature</nature>
22+
</natures>
23+
</projectDescription>
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
eclipse.preferences.version=1
2+
encoding//src/main/java=UTF-8
3+
encoding//src/test/java=UTF-8
4+
encoding/<project>=UTF-8
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
eclipse.preferences.version=1
2+
org.eclipse.jdt.core.compiler.codegen.inlineJsrBytecode=enabled
3+
org.eclipse.jdt.core.compiler.codegen.methodParameters=do not generate
4+
org.eclipse.jdt.core.compiler.codegen.targetPlatform=1.8
5+
org.eclipse.jdt.core.compiler.codegen.unusedLocal=preserve
6+
org.eclipse.jdt.core.compiler.compliance=1.8
7+
org.eclipse.jdt.core.compiler.debug.lineNumber=generate
8+
org.eclipse.jdt.core.compiler.debug.localVariable=generate
9+
org.eclipse.jdt.core.compiler.debug.sourceFile=generate
10+
org.eclipse.jdt.core.compiler.problem.assertIdentifier=error
11+
org.eclipse.jdt.core.compiler.problem.enumIdentifier=error
12+
org.eclipse.jdt.core.compiler.problem.forbiddenReference=warning
13+
org.eclipse.jdt.core.compiler.source=1.8
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
activeProfiles=
2+
eclipse.preferences.version=1
3+
resolveWorkspaceProjects=true
4+
version=1
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
2+
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
3+
<modelVersion>4.0.0</modelVersion>
4+
5+
<groupId>com.eudriscabrera.examples</groupId>
6+
<artifactId>java8-concurrency</artifactId>
7+
<version>1.0-SNAPSHOT</version>
8+
<packaging>jar</packaging>
9+
10+
<name>java8-concurrency</name>
11+
<url>http://maven.apache.org</url>
12+
13+
<properties>
14+
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
15+
</properties>
16+
17+
<dependencies>
18+
<dependency>
19+
<groupId>junit</groupId>
20+
<artifactId>junit</artifactId>
21+
<version>3.8.1</version>
22+
<scope>test</scope>
23+
</dependency>
24+
</dependencies>
25+
</project>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
package com.eudriscabrera.examples.concurrency.java8.tutorial;
2+
3+
import java.util.concurrent.ExecutorService;
4+
import java.util.concurrent.Executors;
5+
6+
/**
7+
* @author ecabrerar
8+
*
9+
*/
10+
public class BasicExecutorService {
11+
public static void main(String[] args) {
12+
13+
ExecutorService executor = Executors.newSingleThreadExecutor();
14+
15+
executor.submit(() -> {
16+
String threadName = Thread.currentThread().getName();
17+
18+
System.out.println("Hello " + threadName);
19+
});
20+
}
21+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
2+
package com.eudriscabrera.examples.concurrency.java8.tutorial;
3+
4+
import java.util.concurrent.Executors;
5+
import java.util.concurrent.ScheduledExecutorService;
6+
import java.util.concurrent.ScheduledFuture;
7+
import java.util.concurrent.TimeUnit;
8+
9+
/**
10+
* @author ecabrerar
11+
*
12+
*/
13+
public class BasicScheduledExecutorService {
14+
15+
16+
public static void main(String[] args) throws InterruptedException {
17+
ScheduledExecutorService executor = Executors.newScheduledThreadPool(1);
18+
19+
Runnable task = () -> System.out.println("Scheduling: " + System.nanoTime());
20+
ScheduledFuture<?> future = executor.schedule(task, 3, TimeUnit.SECONDS);
21+
22+
TimeUnit.MILLISECONDS.sleep(1337);
23+
24+
long remainingDelay = future.getDelay(TimeUnit.MILLISECONDS);
25+
System.out.printf("Remaining Delay: %sms", remainingDelay);
26+
27+
}
28+
29+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
package com.eudriscabrera.examples.concurrency.java8.tutorial;
2+
3+
4+
public class BasicThread {
5+
public static void main(String[] args) {
6+
7+
8+
Runnable task = () -> {
9+
10+
String threadName = Thread.currentThread().getName();
11+
12+
System.out.println("Hello " + threadName);
13+
14+
};
15+
16+
task.run();
17+
18+
Thread thread = new Thread(task);
19+
thread.start();
20+
21+
System.out.println("Done!");
22+
23+
}
24+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
package com.eudriscabrera.examples.concurrency.java8.tutorial;
2+
3+
import java.util.concurrent.TimeUnit;
4+
5+
/**
6+
* @author ecabrerar
7+
*
8+
*/
9+
public class BasicThreadWithSleepTime {
10+
11+
public static void main(String[] args) {
12+
Runnable task = () -> {
13+
14+
try {
15+
String name = Thread.currentThread().getName();
16+
17+
System.out.println("Foo " + name);
18+
TimeUnit.SECONDS.sleep(1);
19+
System.out.println("Bar " + name);
20+
21+
} catch (InterruptedException e) {
22+
e.printStackTrace();
23+
}
24+
25+
};
26+
27+
task.run();
28+
29+
Thread thread = new Thread(task);
30+
thread.start();
31+
32+
}
33+
34+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
package com.eudriscabrera.examples.concurrency.java8.tutorial;
2+
3+
import java.util.concurrent.Callable;
4+
import java.util.concurrent.ExecutionException;
5+
import java.util.concurrent.ExecutorService;
6+
import java.util.concurrent.Executors;
7+
import java.util.concurrent.Future;
8+
import java.util.concurrent.TimeUnit;
9+
10+
/**
11+
*
12+
* @author ecabrerar
13+
*
14+
*/
15+
16+
public class ExecutorServiceWithCallableAndFuture {
17+
18+
public static void main(String[] args) {
19+
20+
Callable<Integer> task = ()->{
21+
try {
22+
TimeUnit.SECONDS.sleep(1);
23+
return 123;
24+
}
25+
catch (InterruptedException e) {
26+
throw new IllegalStateException("task interrupted", e);
27+
}
28+
29+
};
30+
31+
ExecutorService executor = Executors.newFixedThreadPool(1);
32+
Future<Integer> future = executor.submit(task);
33+
34+
System.out.println("future done? " + future.isDone());
35+
36+
Integer result = 0;
37+
38+
try {
39+
result = future.get();
40+
} catch (InterruptedException | ExecutionException e) {
41+
e.printStackTrace();
42+
}
43+
44+
System.out.println("future done? " + future.isDone());
45+
System.out.print("result: " + result);
46+
47+
48+
}
49+
50+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
package com.eudriscabrera.examples.concurrency.java8.tutorial;
2+
3+
import java.util.concurrent.ExecutionException;
4+
import java.util.concurrent.ExecutorService;
5+
import java.util.concurrent.Executors;
6+
import java.util.concurrent.Future;
7+
import java.util.concurrent.TimeUnit;
8+
import java.util.concurrent.TimeoutException;
9+
10+
/**
11+
*
12+
* @author ecabrerar
13+
*
14+
*/
15+
16+
public class ExecutorServiceWithCallableAndFutureAndTimeouts {
17+
18+
public static void main(String[] args) {
19+
20+
ExecutorService executor = Executors.newFixedThreadPool(1);
21+
Future<Integer> future = executor.submit(()->{
22+
try {
23+
TimeUnit.SECONDS.sleep(2);
24+
return 123;
25+
}
26+
catch (InterruptedException e) {
27+
throw new IllegalStateException("task interrupted", e);
28+
}
29+
30+
});
31+
32+
System.out.println("future done? " + future.isDone());
33+
34+
Integer result = 0;
35+
36+
try {
37+
result = future.get(2,TimeUnit.SECONDS);
38+
} catch (InterruptedException | ExecutionException | TimeoutException e) {
39+
e.printStackTrace();
40+
}
41+
42+
System.out.println("future done? " + future.isDone());
43+
System.out.print("result: " + result);
44+
45+
46+
}
47+
48+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
package com.eudriscabrera.examples.concurrency.java8.tutorial;
2+
3+
import java.util.Arrays;
4+
import java.util.List;
5+
import java.util.concurrent.Callable;
6+
import java.util.concurrent.ExecutorService;
7+
import java.util.concurrent.Executors;
8+
9+
/**
10+
*
11+
* @author ecabrerar
12+
*
13+
*/
14+
15+
public class ExecutorServiceWithCallableAndInvokeAll {
16+
17+
public static void main(String[] args) throws InterruptedException {
18+
19+
20+
ExecutorService executor = Executors.newWorkStealingPool();
21+
22+
List<Callable<String>> callables = Arrays.asList(
23+
() -> "task1",
24+
() -> "task2",
25+
() -> "task3");
26+
27+
executor.invokeAll(callables)
28+
.stream()
29+
.map(future -> {
30+
try {
31+
return future.get();
32+
}
33+
catch (Exception e) {
34+
throw new IllegalStateException(e);
35+
}
36+
}
37+
).forEach(System.out::println);
38+
}
39+
40+
}

0 commit comments

Comments
 (0)