Skip to content

Commit fddbd1b

Browse files
java async method demo
1 parent 6c56da1 commit fddbd1b

File tree

8 files changed

+194
-0
lines changed

8 files changed

+194
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
package sporadic.java_async_method_example.async.method;
2+
3+
public interface AsyncClient {
4+
5+
// for asynchronous
6+
public void executeAsynchronous(final String userId);
7+
8+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
package sporadic.java_async_method_example.async.method;
2+
3+
import java.util.ArrayList;
4+
import java.util.Date;
5+
import java.util.List;
6+
import java.util.concurrent.Callable;
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+
public class AsyncClientImpl implements AsyncClient {
13+
ExecutorService executor = Executors.newFixedThreadPool(3);
14+
15+
@Override
16+
public void executeAsynchronous(String userId) {
17+
List<Future<String>> list = new ArrayList<Future<String>>();
18+
19+
Callable<String> callable = new Task(userId);
20+
21+
for(int i = 0; i < 10; i++) {
22+
Future<String> future = executor.submit(callable);
23+
list.add(future);
24+
}
25+
26+
for(Future<String> future : list){
27+
try {
28+
System.out.println(new Date() + " " + future.get());
29+
} catch (InterruptedException | ExecutionException e) {
30+
e.printStackTrace();
31+
}
32+
}
33+
System.out.println("That's the end of the executeAsynchronous method!");
34+
executor.shutdown();
35+
}
36+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
package sporadic.java_async_method_example.async.method;
2+
3+
/**This package was used to demo the difference between sync and async methods, but right now, there's really no difference between the impl of SyncClientImpl and AsyncClientImpl classes,
4+
* I need to rewrite them to deepen my understanding!*/
5+
public class MainApp {
6+
7+
public static void main(String... args) {
8+
SyncClient syncClient = new SyncClientImpl();
9+
10+
syncClient.executeSynchronous("this is executing synchronous method!");
11+
12+
AsyncClient asyncClient = new AsyncClientImpl();
13+
asyncClient
14+
.executeAsynchronous("this is executing Asynchronous method!");
15+
16+
System.out.println("That's the end of MainApp!");
17+
}
18+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
package sporadic.java_async_method_example.async.method;
2+
3+
public interface SyncClient {
4+
// for synchronous
5+
public void executeSynchronous(final String userId);
6+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
package sporadic.java_async_method_example.async.method;
2+
3+
import java.util.ArrayList;
4+
import java.util.Date;
5+
import java.util.List;
6+
import java.util.concurrent.Callable;
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+
public class SyncClientImpl implements SyncClient {
13+
ExecutorService executor = Executors.newFixedThreadPool(3);
14+
15+
@Override
16+
public void executeSynchronous(String userId) {
17+
List<Future<String>> list = new ArrayList<Future<String>>();
18+
19+
Callable<String> callable = new Task(userId);
20+
21+
for(int i = 0; i < 10; i++) {
22+
Future<String> future = executor.submit(callable);
23+
list.add(future);
24+
}
25+
26+
for(Future<String> future : list){
27+
try {
28+
System.out.println(new Date() + " " + future.get());
29+
} catch (InterruptedException | ExecutionException e) {
30+
e.printStackTrace();
31+
}
32+
}
33+
executor.shutdown();
34+
System.out.println("That's the end of the executeSynchronous method!");
35+
}
36+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
package sporadic.java_async_method_example.async.method;
2+
3+
import java.util.concurrent.Callable;
4+
5+
public class Task implements Callable<String> {
6+
7+
private final String userId;
8+
9+
public Task(String userId) {
10+
this.userId = userId;
11+
}
12+
13+
@Override
14+
public String call() throws Exception {
15+
Thread.sleep(1500);
16+
return Thread.currentThread().getName() + " " + userId;
17+
}
18+
19+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
package sporadic.java_async_method_example.future;
2+
3+
import java.util.ArrayList;
4+
import java.util.Date;
5+
import java.util.List;
6+
import java.util.concurrent.Callable;
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+
public class MainApp {
13+
14+
public static void main(String... args) {
15+
//Played around with different Executors, have different effects, pretty cool!
16+
ExecutorService executor = //Executors.newFixedThreadPool(5)
17+
// Executors.newSingleThreadExecutor()
18+
// Executors.newCachedThreadPool()
19+
Executors.newScheduledThreadPool(15);
20+
/**thread pool account could be a bottleneck when it's smaller than 10 which is the max in the below for loop.
21+
* so when I changed the ThreadPool size to 15, then ALL Future objects got returned at the same time! Cool!*/
22+
23+
List<Future<String>> list = new ArrayList<Future<String>>();
24+
25+
Callable<String> callable = new MyCallable();
26+
27+
for (int i = 0; i < 10; i++) {
28+
Future<String> future = executor.submit(callable);
29+
while (!future.isDone()) {
30+
try {
31+
Thread.sleep(500);
32+
} catch (InterruptedException e) {
33+
e.printStackTrace();
34+
}
35+
System.out.println("callable: " + callable + " is not done yet, please wait...");
36+
}
37+
System.out.println("callable: " + callable + " is already done and is being added into the list.\n");
38+
list.add(future);
39+
}
40+
41+
for(Future<String> future : list){
42+
try {
43+
System.out.println(new Date() + " " + future.get());
44+
} catch (InterruptedException | ExecutionException e) {
45+
e.printStackTrace();
46+
}
47+
}
48+
executor.shutdown();
49+
System.out.println("That's the end of the program!");
50+
}
51+
52+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
package sporadic.java_async_method_example.future;
2+
3+
import java.util.concurrent.Callable;
4+
5+
/**
6+
* This class is used to help me understand Future class, Future class must be assigned from the Executors.
7+
* class's submit() method to process an argument which implements Callable interface,
8+
* that's why I have this class called MyCallable here!*/
9+
public class MyCallable implements Callable {
10+
11+
@Override
12+
public String call() throws Exception {
13+
Thread.sleep(1500);
14+
String threadName = Thread.currentThread().getName();
15+
System.out.println(threadName + " is being called now!!!");
16+
return threadName;
17+
}
18+
19+
}

0 commit comments

Comments
 (0)