Skip to content

Commit ec9e0a1

Browse files
committed
callback
1 parent c65639e commit ec9e0a1

File tree

3 files changed

+71
-6
lines changed

3 files changed

+71
-6
lines changed
Lines changed: 63 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,74 @@
11
package concurrency.future;
22

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.FutureTask;
8+
import lombok.extern.slf4j.Slf4j;
9+
10+
@Slf4j
311
public class CallbackEx {
412

5-
interface Callback {
6-
void onSuccess();
13+
interface SuccessCallBack<T> {
14+
void onSuccess(T t);
15+
}
16+
17+
interface ExceptionCallBack {
18+
void onError(Throwable t);
19+
}
20+
21+
static class CallbackFutureTask<T> extends FutureTask<T> {
22+
23+
private final SuccessCallBack<T> sc;
24+
private final ExceptionCallBack ec;
25+
public CallbackFutureTask(Callable<T> callable, SuccessCallBack<T> onSuccess, ExceptionCallBack onError) {
26+
super(callable);
27+
this.sc = onSuccess;
28+
this.ec = onError;
29+
}
30+
31+
@Override
32+
protected void done() {
33+
try {
34+
sc.onSuccess(get());
35+
} catch (InterruptedException e) {
36+
throw new RuntimeException(e);
37+
} catch (ExecutionException e) {
38+
ec.onError(e);
39+
}
40+
}
741
}
842

9-
public static void main(String[] args) {
43+
public static void main(String[] args) throws InterruptedException {
44+
log.debug("Enter");
45+
final ExecutorService executorService = Executors.newCachedThreadPool();
46+
47+
final long startTime = System.currentTimeMillis();
48+
49+
// non-blocking and asynchronous and
50+
CallbackFutureTask<String> future = new CallbackFutureTask<>(
51+
() -> {
52+
log.debug("Processing task asynchronously");
53+
try {
54+
Thread.sleep(2000);
55+
} catch (InterruptedException e) {
56+
throw new RuntimeException(e);
57+
}
58+
59+
return "AsyncResult";
60+
},
61+
s -> log.info("Result: " + s),
62+
e -> log.error("Error: " + e.getMessage())
63+
);
1064

65+
executorService.execute(future);
66+
executorService.shutdown();
1167

68+
log.debug("Is the async task done? " + future.isDone());
69+
log.debug("Processing the other task");
70+
Thread.sleep(1000);
71+
log.debug("Exit " + (System.currentTimeMillis() - startTime));
1272
}
1373

1474
}

src/main/java/concurrency/future/FutureEx.java

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,9 @@ public static void main(String[] args) throws InterruptedException, ExecutionExc
3232
Thread.sleep(1000);
3333

3434
log.debug("Is the async task done? " + future.isDone());
35-
System.out.println("The async result is " + future.get()); // blocking, wait the async result.
35+
// blocking, wait the async result.
36+
// business logic is messed up with non-business logic
37+
log.info("The async result is " + future.get());
3638
log.debug("Exit " + (System.currentTimeMillis() - startTime));
3739
}
3840
}

src/main/java/concurrency/future/FutureTaskEx.java

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,9 @@ public static void main(String[] args) throws InterruptedException, ExecutionExc
1515
final ExecutorService executorService = Executors.newCachedThreadPool();
1616

1717
final long startTime = System.currentTimeMillis();
18+
19+
// non-blocking and asynchronous
20+
// similar to callback style
1821
FutureTask<String> future = new FutureTask<>(
1922
() -> {
2023
log.debug("Processing task asynchronously");
@@ -30,9 +33,9 @@ public static void main(String[] args) throws InterruptedException, ExecutionExc
3033
@Override
3134
protected void done() {
3235
try {
33-
System.out.println("The async result is " + get());
36+
log.info("Result: " + get());
3437
} catch (InterruptedException | ExecutionException e) {
35-
throw new RuntimeException(e);
38+
log.error("Error: " + e.getMessage());
3639
}
3740
}
3841
};

0 commit comments

Comments
 (0)