Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commitfddbd1b

Browse files
java async method demo
1 parent6c56da1 commitfddbd1b

File tree

8 files changed

+194
-0
lines changed

8 files changed

+194
-0
lines changed
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
packagesporadic.java_async_method_example.async.method;
2+
3+
publicinterfaceAsyncClient {
4+
5+
// for asynchronous
6+
publicvoidexecuteAsynchronous(finalStringuserId);
7+
8+
}
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
packagesporadic.java_async_method_example.async.method;
2+
3+
importjava.util.ArrayList;
4+
importjava.util.Date;
5+
importjava.util.List;
6+
importjava.util.concurrent.Callable;
7+
importjava.util.concurrent.ExecutionException;
8+
importjava.util.concurrent.ExecutorService;
9+
importjava.util.concurrent.Executors;
10+
importjava.util.concurrent.Future;
11+
12+
publicclassAsyncClientImplimplementsAsyncClient {
13+
ExecutorServiceexecutor =Executors.newFixedThreadPool(3);
14+
15+
@Override
16+
publicvoidexecuteAsynchronous(StringuserId) {
17+
List<Future<String>>list =newArrayList<Future<String>>();
18+
19+
Callable<String>callable =newTask(userId);
20+
21+
for(inti =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(newDate() +" " +future.get());
29+
}catch (InterruptedException |ExecutionExceptione) {
30+
e.printStackTrace();
31+
}
32+
}
33+
System.out.println("That's the end of the executeAsynchronous method!");
34+
executor.shutdown();
35+
}
36+
}
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
packagesporadic.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+
publicclassMainApp {
6+
7+
publicstaticvoidmain(String...args) {
8+
SyncClientsyncClient =newSyncClientImpl();
9+
10+
syncClient.executeSynchronous("this is executing synchronous method!");
11+
12+
AsyncClientasyncClient =newAsyncClientImpl();
13+
asyncClient
14+
.executeAsynchronous("this is executing Asynchronous method!");
15+
16+
System.out.println("That's the end of MainApp!");
17+
}
18+
}
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
packagesporadic.java_async_method_example.async.method;
2+
3+
publicinterfaceSyncClient {
4+
// for synchronous
5+
publicvoidexecuteSynchronous(finalStringuserId);
6+
}
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
packagesporadic.java_async_method_example.async.method;
2+
3+
importjava.util.ArrayList;
4+
importjava.util.Date;
5+
importjava.util.List;
6+
importjava.util.concurrent.Callable;
7+
importjava.util.concurrent.ExecutionException;
8+
importjava.util.concurrent.ExecutorService;
9+
importjava.util.concurrent.Executors;
10+
importjava.util.concurrent.Future;
11+
12+
publicclassSyncClientImplimplementsSyncClient {
13+
ExecutorServiceexecutor =Executors.newFixedThreadPool(3);
14+
15+
@Override
16+
publicvoidexecuteSynchronous(StringuserId) {
17+
List<Future<String>>list =newArrayList<Future<String>>();
18+
19+
Callable<String>callable =newTask(userId);
20+
21+
for(inti =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(newDate() +" " +future.get());
29+
}catch (InterruptedException |ExecutionExceptione) {
30+
e.printStackTrace();
31+
}
32+
}
33+
executor.shutdown();
34+
System.out.println("That's the end of the executeSynchronous method!");
35+
}
36+
}
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
packagesporadic.java_async_method_example.async.method;
2+
3+
importjava.util.concurrent.Callable;
4+
5+
publicclassTaskimplementsCallable<String> {
6+
7+
privatefinalStringuserId;
8+
9+
publicTask(StringuserId) {
10+
this.userId =userId;
11+
}
12+
13+
@Override
14+
publicStringcall()throwsException {
15+
Thread.sleep(1500);
16+
returnThread.currentThread().getName() +" " +userId;
17+
}
18+
19+
}
Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
packagesporadic.java_async_method_example.future;
2+
3+
importjava.util.ArrayList;
4+
importjava.util.Date;
5+
importjava.util.List;
6+
importjava.util.concurrent.Callable;
7+
importjava.util.concurrent.ExecutionException;
8+
importjava.util.concurrent.ExecutorService;
9+
importjava.util.concurrent.Executors;
10+
importjava.util.concurrent.Future;
11+
12+
publicclassMainApp {
13+
14+
publicstaticvoidmain(String...args) {
15+
//Played around with different Executors, have different effects, pretty cool!
16+
ExecutorServiceexecutor =//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 =newArrayList<Future<String>>();
24+
25+
Callable<String>callable =newMyCallable();
26+
27+
for (inti =0;i <10;i++) {
28+
Future<String>future =executor.submit(callable);
29+
while (!future.isDone()) {
30+
try {
31+
Thread.sleep(500);
32+
}catch (InterruptedExceptione) {
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(newDate() +" " +future.get());
44+
}catch (InterruptedException |ExecutionExceptione) {
45+
e.printStackTrace();
46+
}
47+
}
48+
executor.shutdown();
49+
System.out.println("That's the end of the program!");
50+
}
51+
52+
}
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
packagesporadic.java_async_method_example.future;
2+
3+
importjava.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+
publicclassMyCallableimplementsCallable {
10+
11+
@Override
12+
publicStringcall()throwsException {
13+
Thread.sleep(1500);
14+
StringthreadName =Thread.currentThread().getName();
15+
System.out.println(threadName +" is being called now!!!");
16+
returnthreadName;
17+
}
18+
19+
}

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp