Skip to content

Commit 0a369c4

Browse files
authored
Merge pull request #8456 from SmartyAnsh/BAEL-3596_Asynchronous_Method_Calls_in_Java
Bael 3596 asynchronous method calls in java
2 parents d8b0fd5 + 6dc3151 commit 0a369c4

3 files changed

Lines changed: 303 additions & 0 deletions

File tree

core-java-modules/core-java-concurrency-advanced-3/pom.xml

Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,37 @@
2323
<version>${assertj.version}</version>
2424
<scope>test</scope>
2525
</dependency>
26+
27+
<dependency>
28+
<groupId>com.jcabi</groupId>
29+
<artifactId>jcabi-aspects</artifactId>
30+
<version>${jcabi-aspects.version}</version>
31+
</dependency>
32+
33+
<dependency>
34+
<groupId>org.aspectj</groupId>
35+
<artifactId>aspectjrt</artifactId>
36+
<version>${aspectjrt.version}</version>
37+
<scope>runtime</scope>
38+
</dependency>
39+
40+
<dependency>
41+
<groupId>com.google.guava</groupId>
42+
<artifactId>guava</artifactId>
43+
<version>${guava.version}</version>
44+
</dependency>
45+
46+
<dependency>
47+
<groupId>org.cactoos</groupId>
48+
<artifactId>cactoos</artifactId>
49+
<version>${cactoos.version}</version>
50+
</dependency>
51+
52+
<dependency>
53+
<groupId>com.ea.async</groupId>
54+
<artifactId>ea-async</artifactId>
55+
<version>${ea-async.version}</version>
56+
</dependency>
2657
</dependencies>
2758

2859
<build>
@@ -36,6 +67,30 @@
3667
<target>${maven.compiler.target}</target>
3768
</configuration>
3869
</plugin>
70+
<plugin>
71+
<groupId>com.jcabi</groupId>
72+
<artifactId>jcabi-maven-plugin</artifactId>
73+
<version>${jcabi-maven-plugin.version}</version>
74+
<executions>
75+
<execution>
76+
<goals>
77+
<goal>ajc</goal>
78+
</goals>
79+
</execution>
80+
</executions>
81+
<dependencies>
82+
<dependency>
83+
<groupId>org.aspectj</groupId>
84+
<artifactId>aspectjtools</artifactId>
85+
<version>${aspectjtools.version}</version>
86+
</dependency>
87+
<dependency>
88+
<groupId>org.aspectj</groupId>
89+
<artifactId>aspectjweaver</artifactId>
90+
<version>${aspectjweaver.version}</version>
91+
</dependency>
92+
</dependencies>
93+
</plugin>
3994
</plugins>
4095
<resources>
4196
<resource>
@@ -49,6 +104,14 @@
49104
<assertj.version>3.14.0</assertj.version>
50105
<maven.compiler.source>1.8</maven.compiler.source>
51106
<maven.compiler.target>1.8</maven.compiler.target>
107+
<jcabi-aspects.version>0.22.6</jcabi-aspects.version>
108+
<aspectjrt.version>1.9.5</aspectjrt.version>
109+
<guava.version>28.2-jre</guava.version>
110+
<cactoos.version>0.43</cactoos.version>
111+
<ea-async.version>1.2.3</ea-async.version>
112+
<jcabi-maven-plugin.version>0.14.1</jcabi-maven-plugin.version>
113+
<aspectjtools.version>1.9.1</aspectjtools.version>
114+
<aspectjweaver.version>1.9.1</aspectjweaver.version>
52115
</properties>
53116

54117
</project>
Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
package com.baeldung.async;
2+
3+
import static com.ea.async.Async.await;
4+
import java.util.concurrent.CompletableFuture;
5+
import java.util.concurrent.ExecutionException;
6+
7+
import com.ea.async.Async;
8+
9+
public class EAAsyncExample {
10+
11+
static {
12+
Async.init();
13+
}
14+
15+
public static void main(String[] args) throws Exception {
16+
usingCompletableFuture();
17+
usingAsyncAwait();
18+
}
19+
20+
public static void usingCompletableFuture() throws InterruptedException, ExecutionException, Exception {
21+
CompletableFuture<Void> completableFuture = hello()
22+
.thenComposeAsync(hello -> mergeWorld(hello))
23+
.thenAcceptAsync(helloWorld -> print(helloWorld))
24+
.exceptionally( throwable -> {
25+
System.out.println(throwable.getCause());
26+
return null;
27+
});
28+
completableFuture.get();
29+
}
30+
31+
public static CompletableFuture<String> hello() {
32+
CompletableFuture<String> completableFuture = CompletableFuture.supplyAsync(() -> "Hello");
33+
return completableFuture;
34+
}
35+
36+
public static CompletableFuture<String> mergeWorld(String s) {
37+
CompletableFuture<String> completableFuture = CompletableFuture.supplyAsync(() -> {
38+
return s + " World!";
39+
});
40+
return completableFuture;
41+
}
42+
43+
public static void print(String str) {
44+
CompletableFuture.runAsync(() -> System.out.println(str));
45+
}
46+
47+
private static void usingAsyncAwait() {
48+
try {
49+
String hello = await(hello());
50+
String helloWorld = await(mergeWorld(hello));
51+
await(CompletableFuture.runAsync(() -> print(helloWorld)));
52+
} catch (Exception e) {
53+
e.printStackTrace();
54+
}
55+
}
56+
57+
}
Lines changed: 183 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,183 @@
1+
package com.baeldung.async;
2+
3+
import static com.ea.async.Async.await;
4+
5+
import java.util.concurrent.Callable;
6+
import java.util.concurrent.CompletableFuture;
7+
import java.util.concurrent.ExecutionException;
8+
import java.util.concurrent.ExecutorService;
9+
import java.util.concurrent.Executors;
10+
import java.util.concurrent.Future;
11+
12+
import com.google.common.util.concurrent.AsyncCallable;
13+
import com.google.common.util.concurrent.Callables;
14+
import com.google.common.util.concurrent.Futures;
15+
import com.google.common.util.concurrent.ListenableFuture;
16+
import com.google.common.util.concurrent.ListeningExecutorService;
17+
import com.google.common.util.concurrent.MoreExecutors;
18+
import com.jcabi.aspects.Async;
19+
import com.jcabi.aspects.Loggable;
20+
21+
public class JavaAsync {
22+
23+
static {
24+
com.ea.async.Async.init();
25+
}
26+
27+
private static final ExecutorService threadpool = Executors.newCachedThreadPool();
28+
29+
public static void main (String[] args) throws InterruptedException, ExecutionException {
30+
int number = 20;
31+
32+
//Thread Example
33+
factorialUsingThread(number).start();
34+
35+
//FutureTask Example
36+
Future<Long> futureTask = factorialUsingFutureTask(number);
37+
System.out.println("Factorial of " + number + " is: " + futureTask.get());
38+
39+
// CompletableFuture Example
40+
Future<Long> completableFuture = factorialUsingCompletableFuture(number);
41+
System.out.println("Factorial of " + number + " is: " + completableFuture.get());
42+
43+
// EA Async example
44+
System.out.println("Factorial of " + number + " is: " + factorialUsingEAAsync(number));
45+
46+
// cactoos async example
47+
Future<Long> asyncFuture = factorialUsingCactoos(number);
48+
System.out.println("Factorial of " + number + " is: " + asyncFuture.get());
49+
50+
// Guava example
51+
ListenableFuture<Long> guavaFuture = factorialUsingGuavaServiceSubmit(number);
52+
System.out.println("Factorial of " + number + " is: " + guavaFuture.get());
53+
54+
ListenableFuture<Long> guavaFutures = factorialUsingGuavaFutures(number);
55+
System.out.println("Factorial of " + number + " is: " + guavaFutures.get());
56+
57+
// @async jcabi-aspect example
58+
Future<Long> aspectFuture = factorialUsingJcabiAspect(number);
59+
System.out.println("Factorial of " + number + " is: " + aspectFuture.get());
60+
61+
}
62+
63+
/**
64+
* Finds factorial of a number
65+
* @param number
66+
* @return
67+
*/
68+
public static long factorial(int number) {
69+
long result = 1;
70+
for(int i=number;i>0;i--) {
71+
result *= i;
72+
}
73+
return result;
74+
}
75+
76+
/**
77+
* Finds factorial of a number using Thread
78+
* @param number
79+
* @return
80+
*/
81+
@Loggable
82+
public static Thread factorialUsingThread(int number) {
83+
Thread newThread = new Thread(() -> {
84+
System.out.println("Factorial of " + number + " is: " + factorial(number));
85+
});
86+
87+
return newThread;
88+
}
89+
90+
/**
91+
* Finds factorial of a number using FutureTask
92+
* @param number
93+
* @return
94+
*/
95+
@Loggable
96+
public static Future<Long> factorialUsingFutureTask(int number) {
97+
Future<Long> futureTask = threadpool.submit(() -> factorial(number));
98+
99+
while (!futureTask.isDone()) {
100+
System.out.println("FutureTask is not finished yet...");
101+
}
102+
103+
return futureTask;
104+
}
105+
106+
/**
107+
* Finds factorial of a number using CompletableFuture
108+
* @param number
109+
* @return
110+
*/
111+
@Loggable
112+
public static Future<Long> factorialUsingCompletableFuture(int number) {
113+
CompletableFuture<Long> completableFuture = CompletableFuture.supplyAsync(() -> factorial(number));
114+
return completableFuture;
115+
}
116+
117+
/**
118+
* Finds factorial of a number using EA Async
119+
* @param number
120+
* @return
121+
*/
122+
@Loggable
123+
public static long factorialUsingEAAsync(int number) {
124+
CompletableFuture<Long> completableFuture = CompletableFuture.supplyAsync(() -> factorial(number));
125+
long result = await(completableFuture);
126+
return result;
127+
}
128+
129+
/**
130+
* Finds factorial of a number using Async of Cactoos
131+
* @param number
132+
* @return
133+
* @throws InterruptedException
134+
* @throws ExecutionException
135+
*/
136+
@Loggable
137+
public static Future<Long> factorialUsingCactoos(int number) throws InterruptedException, ExecutionException {
138+
org.cactoos.func.Async<Integer, Long> asyncFunction = new org.cactoos.func.Async<Integer, Long>(input -> factorial(input));
139+
Future<Long> asyncFuture = asyncFunction.apply(number);
140+
return asyncFuture;
141+
}
142+
143+
/**
144+
* Finds factorial of a number using Guava's ListeningExecutorService.submit()
145+
* @param number
146+
* @return
147+
*/
148+
@Loggable
149+
public static ListenableFuture<Long> factorialUsingGuavaServiceSubmit(int number) {
150+
ListeningExecutorService service = MoreExecutors.listeningDecorator(threadpool);
151+
ListenableFuture<Long> factorialFuture = (ListenableFuture<Long>) service.submit(()-> factorial(number));
152+
return factorialFuture;
153+
}
154+
155+
/**
156+
* Finds factorial of a number using Guava's Futures.submitAsync()
157+
* @param number
158+
* @return
159+
*/
160+
@Loggable
161+
public static ListenableFuture<Long> factorialUsingGuavaFutures(int number) {
162+
ListeningExecutorService service = MoreExecutors.listeningDecorator(threadpool);
163+
AsyncCallable<Long> asyncCallable = Callables.asAsyncCallable(new Callable<Long>() {
164+
public Long call() {
165+
return factorial(number);
166+
}
167+
}, service);
168+
return Futures.submitAsync(asyncCallable, service);
169+
}
170+
171+
/**
172+
* Finds factorial of a number using @Async of jcabi-aspects
173+
* @param number
174+
* @return
175+
*/
176+
@Async
177+
@Loggable
178+
public static Future<Long> factorialUsingJcabiAspect(int number) {
179+
Future<Long> factorialFuture = CompletableFuture.supplyAsync(() -> factorial(number));
180+
return factorialFuture;
181+
}
182+
183+
}

0 commit comments

Comments
 (0)