Skip to content

Commit 8251c25

Browse files
committed
Merge remote-tracking branch 'origin/master'
2 parents 6e08121 + 2327379 commit 8251c25

54 files changed

Lines changed: 2444 additions & 47 deletions

File tree

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
package com.baeldung.threadpool;
2+
3+
import java.util.concurrent.ForkJoinTask;
4+
import java.util.concurrent.RecursiveTask;
5+
import java.util.stream.Collectors;
6+
7+
public class CountingTask extends RecursiveTask<Integer> {
8+
9+
private final TreeNode node;
10+
11+
public CountingTask(TreeNode node) {
12+
this.node = node;
13+
}
14+
15+
@Override
16+
protected Integer compute() {
17+
return node.value + node.children.stream()
18+
.map(childNode -> new CountingTask(childNode).fork())
19+
.collect(Collectors.summingInt(ForkJoinTask::join));
20+
}
21+
22+
}
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
package com.baeldung.threadpool;
2+
3+
import java.util.concurrent.ExecutorService;
4+
import java.util.concurrent.Executors;
5+
import java.util.concurrent.ThreadPoolExecutor;
6+
import java.util.concurrent.TimeUnit;
7+
8+
import com.google.common.util.concurrent.MoreExecutors;
9+
10+
/**
11+
* This class demonstrates the usage of Guava's exiting executor services that keep the VM from hanging.
12+
* Without the exiting executor service, the task would hang indefinitely.
13+
* This behaviour cannot be demonstrated in JUnit tests, as JUnit kills the VM after the tests.
14+
*/
15+
public class ExitingExecutorServiceExample {
16+
17+
public static void main(String... args) {
18+
19+
ThreadPoolExecutor executor = (ThreadPoolExecutor) Executors.newFixedThreadPool(5);
20+
ExecutorService executorService = MoreExecutors.getExitingExecutorService(executor, 100, TimeUnit.MILLISECONDS);
21+
22+
executorService.submit(() -> {
23+
while (true) {
24+
}
25+
});
26+
27+
}
28+
29+
}
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
package com.baeldung.threadpool;
2+
3+
import java.util.Arrays;
4+
import java.util.HashSet;
5+
import java.util.Set;
6+
7+
public class TreeNode {
8+
9+
int value;
10+
11+
Set<TreeNode> children;
12+
13+
public TreeNode(int value, TreeNode... children) {
14+
this.value = value;
15+
this.children = new HashSet<>();
16+
this.children.addAll(Arrays.asList(children));
17+
}
18+
19+
}
Lines changed: 146 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,146 @@
1+
package com.baeldung.threadpool;
2+
3+
import java.util.concurrent.*;
4+
import java.util.concurrent.atomic.AtomicInteger;
5+
6+
import org.junit.Test;
7+
8+
import static org.junit.Assert.assertEquals;
9+
10+
public class CoreThreadPoolTest {
11+
12+
@Test(timeout = 1000)
13+
public void whenCallingExecuteWithRunnable_thenRunnableIsExecuted() throws InterruptedException {
14+
15+
CountDownLatch lock = new CountDownLatch(1);
16+
17+
Executor executor = Executors.newSingleThreadExecutor();
18+
executor.execute(() -> {
19+
System.out.println("Hello World");
20+
lock.countDown();
21+
});
22+
23+
lock.await(1000, TimeUnit.MILLISECONDS);
24+
}
25+
26+
@Test
27+
public void whenUsingExecutorServiceAndFuture_thenCanWaitOnFutureResult() throws InterruptedException, ExecutionException {
28+
29+
ExecutorService executorService = Executors.newFixedThreadPool(10);
30+
Future<String> future = executorService.submit(() -> "Hello World");
31+
String result = future.get();
32+
33+
assertEquals("Hello World", result);
34+
35+
}
36+
37+
@Test
38+
public void whenUsingFixedThreadPool_thenCoreAndMaximumThreadSizeAreTheSame() {
39+
40+
ThreadPoolExecutor executor = (ThreadPoolExecutor) Executors.newFixedThreadPool(2);
41+
executor.submit(() -> {
42+
Thread.sleep(1000);
43+
return null;
44+
});
45+
executor.submit(() -> {
46+
Thread.sleep(1000);
47+
return null;
48+
});
49+
executor.submit(() -> {
50+
Thread.sleep(1000);
51+
return null;
52+
});
53+
54+
assertEquals(2, executor.getPoolSize());
55+
assertEquals(1, executor.getQueue().size());
56+
57+
}
58+
59+
@Test
60+
public void whenUsingCachedThreadPool_thenPoolSizeGrowsUnbounded() {
61+
ThreadPoolExecutor executor = (ThreadPoolExecutor) Executors.newCachedThreadPool();
62+
executor.submit(() -> {
63+
Thread.sleep(1000);
64+
return null;
65+
});
66+
executor.submit(() -> {
67+
Thread.sleep(1000);
68+
return null;
69+
});
70+
executor.submit(() -> {
71+
Thread.sleep(1000);
72+
return null;
73+
});
74+
75+
assertEquals(3, executor.getPoolSize());
76+
assertEquals(0, executor.getQueue().size());
77+
78+
}
79+
80+
@Test(timeout = 1000)
81+
public void whenUsingSingleThreadPool_thenTasksExecuteSequentially() throws InterruptedException {
82+
83+
CountDownLatch lock = new CountDownLatch(2);
84+
AtomicInteger counter = new AtomicInteger();
85+
86+
ExecutorService executor = Executors.newSingleThreadExecutor();
87+
executor.submit(() -> {
88+
counter.set(1);
89+
lock.countDown();
90+
});
91+
executor.submit(() -> {
92+
counter.compareAndSet(1, 2);
93+
lock.countDown();
94+
});
95+
96+
lock.await(1000, TimeUnit.MILLISECONDS);
97+
assertEquals(2, counter.get());
98+
99+
}
100+
101+
@Test(timeout = 1000)
102+
public void whenSchedulingTask_thenTaskExecutesWithinGivenPeriod() throws InterruptedException {
103+
104+
CountDownLatch lock = new CountDownLatch(1);
105+
106+
ScheduledExecutorService executor = Executors.newScheduledThreadPool(5);
107+
executor.schedule(() -> {
108+
System.out.println("Hello World");
109+
lock.countDown();
110+
}, 500, TimeUnit.MILLISECONDS);
111+
112+
lock.await(1000, TimeUnit.MILLISECONDS);
113+
114+
}
115+
116+
@Test(timeout = 1000)
117+
public void whenSchedulingTaskWithFixedPeriod_thenTaskExecutesMultipleTimes() throws InterruptedException {
118+
119+
CountDownLatch lock = new CountDownLatch(3);
120+
121+
ScheduledExecutorService executor = Executors.newScheduledThreadPool(5);
122+
ScheduledFuture<?> future = executor.scheduleAtFixedRate(() -> {
123+
System.out.println("Hello World");
124+
lock.countDown();
125+
}, 500, 100, TimeUnit.MILLISECONDS);
126+
127+
lock.await();
128+
future.cancel(true);
129+
130+
}
131+
132+
@Test
133+
public void whenUsingForkJoinPool_thenSumOfTreeElementsIsCalculatedCorrectly() {
134+
135+
TreeNode tree = new TreeNode(5,
136+
new TreeNode(3), new TreeNode(2,
137+
new TreeNode(2), new TreeNode(8)));
138+
139+
ForkJoinPool forkJoinPool = ForkJoinPool.commonPool();
140+
int sum = forkJoinPool.invoke(new CountingTask(tree));
141+
142+
assertEquals(20, sum);
143+
}
144+
145+
146+
}
Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
package com.baeldung.threadpool;
2+
3+
import java.util.concurrent.ExecutionException;
4+
import java.util.concurrent.Executor;
5+
import java.util.concurrent.ExecutorService;
6+
import java.util.concurrent.Executors;
7+
import java.util.concurrent.atomic.AtomicBoolean;
8+
import java.util.stream.Collectors;
9+
10+
import com.google.common.util.concurrent.Futures;
11+
import com.google.common.util.concurrent.ListenableFuture;
12+
import com.google.common.util.concurrent.ListeningExecutorService;
13+
import com.google.common.util.concurrent.MoreExecutors;
14+
import org.junit.Test;
15+
16+
import static org.junit.Assert.assertEquals;
17+
import static org.junit.Assert.assertTrue;
18+
19+
public class GuavaThreadPoolTest {
20+
21+
@Test
22+
public void whenExecutingTaskWithDirectExecutor_thenTheTaskIsExecutedInTheCurrentThread() {
23+
24+
Executor executor = MoreExecutors.directExecutor();
25+
26+
AtomicBoolean executed = new AtomicBoolean();
27+
28+
executor.execute(() -> {
29+
try {
30+
Thread.sleep(500);
31+
} catch (InterruptedException e) {
32+
e.printStackTrace();
33+
}
34+
executed.set(true);
35+
});
36+
37+
assertTrue(executed.get());
38+
}
39+
40+
@Test
41+
public void whenJoiningFuturesWithAllAsList_thenCombinedFutureCompletesAfterAllFuturesComplete() throws ExecutionException, InterruptedException {
42+
43+
ExecutorService executorService = Executors.newCachedThreadPool();
44+
ListeningExecutorService listeningExecutorService = MoreExecutors.listeningDecorator(executorService);
45+
46+
ListenableFuture<String> future1 = listeningExecutorService.submit(() -> "Hello");
47+
ListenableFuture<String> future2 = listeningExecutorService.submit(() -> "World");
48+
49+
String greeting = Futures.allAsList(future1, future2).get()
50+
.stream()
51+
.collect(Collectors.joining(" "));
52+
assertEquals("Hello World", greeting);
53+
54+
}
55+
56+
}

‎hystrix/pom.xml‎

Lines changed: 34 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,15 @@
66
<groupId>com.baeldung</groupId>
77
<artifactId>hystrix</artifactId>
88
<version>1.0</version>
9-
109
<name>hystrix</name>
1110

11+
<parent>
12+
<groupId>org.springframework.boot</groupId>
13+
<artifactId>spring-boot-starter-parent</artifactId>
14+
<version>1.4.0.RELEASE</version>
15+
<relativePath></relativePath>
16+
</parent>
17+
1218
<properties>
1319

1420
<!-- General -->
@@ -32,12 +38,35 @@
3238

3339
<dependencies>
3440

41+
<dependency>
42+
<groupId>org.springframework.boot</groupId>
43+
<artifactId>spring-boot-starter-web</artifactId>
44+
</dependency>
45+
46+
<dependency>
47+
<groupId>org.springframework.boot</groupId>
48+
<artifactId>spring-boot-starter-aop</artifactId>
49+
</dependency>
50+
3551
<dependency>
3652
<groupId>com.netflix.hystrix</groupId>
3753
<artifactId>hystrix-core</artifactId>
3854
<version>${hystrix-core.version}</version>
3955
</dependency>
4056

57+
<dependency>
58+
<groupId>com.netflix.hystrix</groupId>
59+
<artifactId>hystrix-metrics-event-stream</artifactId>
60+
<version>1.3.16</version>
61+
</dependency>
62+
63+
<!-- https://mvnrepository.com/artifact/com.netflix.hystrix/hystrix-dashboard -->
64+
<!--<dependency>
65+
<groupId>com.netflix.hystrix</groupId>
66+
<artifactId>hystrix-dashboard</artifactId>
67+
<version>1.4.3</version>
68+
</dependency>-->
69+
4170
<dependency>
4271
<groupId>com.netflix.rxjava</groupId>
4372
<artifactId>rxjava-core</artifactId>
@@ -62,6 +91,10 @@
6291

6392
<build>
6493
<plugins>
94+
<plugin>
95+
<groupId>org.springframework.boot</groupId>
96+
<artifactId>spring-boot-maven-plugin</artifactId>
97+
</plugin>
6598
<plugin>
6699
<groupId>org.apache.maven.plugins</groupId>
67100
<artifactId>maven-compiler-plugin</artifactId>
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
package com.baeldung.hystrix;
2+
3+
import com.netflix.hystrix.contrib.metrics.eventstream.HystrixMetricsStreamServlet;
4+
import org.springframework.boot.SpringApplication;
5+
import org.springframework.boot.autoconfigure.SpringBootApplication;
6+
import org.springframework.boot.web.servlet.ServletRegistrationBean;
7+
import org.springframework.context.annotation.Bean;
8+
9+
@SpringBootApplication
10+
public class AppConfig {
11+
12+
public static void main(String[] args) {
13+
SpringApplication.run(AppConfig.class, args);
14+
}
15+
16+
@Bean
17+
public ServletRegistrationBean adminServletRegistrationBean() {
18+
return new ServletRegistrationBean(new HystrixMetricsStreamServlet(), "/hystrix.stream");
19+
}
20+
}

hystrix/src/test/java/com/baeldung/hystrix/CommandHelloWorld.java renamed to hystrix/src/main/java/com/baeldung/hystrix/CommandHelloWorld.java

File renamed without changes.

0 commit comments

Comments
 (0)