Skip to content

Commit 44ef536

Browse files
authored
Update README.md
1 parent 000ffec commit 44ef536

File tree

1 file changed

+6
-4
lines changed

1 file changed

+6
-4
lines changed

README.md

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -534,7 +534,7 @@ But before discussing it, it's necessary to explain a difference in each pair of
534534

535535
## 9. Context variables & contextual Promises
536536

537-
Ahh, those dreaded `TreadLocal`-s we all hate, love to hate, but, neveretheless, use everywhere. It's quite common to store some contextual data (like authenticated user and current locale) inside `ThreadLocal` variables. Sometimes it's a custom code, sometimes the code from third-party library we can't alter.
537+
Ah, those dreaded `TreadLocal`-s we all hate, love to hate, but, neveretheless, use everywhere. It's quite common to store some contextual data (like authenticated user and current locale) inside `ThreadLocal` variables. Sometimes it's a custom code, sometimes the code from third-party library we can't alter.
538538

539539
Typically, we spawn asynchronous code from some thread with well-known characteristics, like HTTP request thread. Here we can easly access contextual information from thread-local variables. However, using thread-local variables from asynchronous code block is hard while it's impossible to predict what thread from the pool will execute the code. It's necessary to capture the context of the one thread and propagate it to threads executing asynchronous code.
540540

@@ -562,14 +562,16 @@ Promise<Void> httpRequest =
562562
CompletableTask.supplyAsync(() -> getDownloadUrlTemplate(), myExecutor)
563563
.as(newContextualPromise) // <--- HERE the conversion is set
564564
.thenApply(url ->
565-
url + "?user=" + MyService.CURRENT_PRINCIPAL.name() + "&locale" + MyService.CURRENT_LOCALE.toString()
565+
url +
566+
"?user=" + MyService.CURRENT_PRINCIPAL.getName() +
567+
"&locale=" + MyService.CURRENT_LOCALE.toString()
566568
)
567569
.thenAccept(url -> {
568-
log.info("Get data for user " + MyService.CURRENT_PRINCIPAL.name());
570+
log.info("Get data for user " + MyService.CURRENT_PRINCIPAL.getName());
569571
executeHttpRequest(url);
570572
});
571573
```
572-
In example above, after the call to `Promise.as(newContextualPromise)` all of the derrived promises may access contextual information captured in originated thread (i.e. code blocks in thenApply / thenAccept).
574+
In the example above, after the call to `Promise.as(newContextualPromise)` all of the derrived promises (i.e. code blocks in thenApply / thenAccept) may access contextual information captured in the originated thread.
573575

574576
Worth to mention, that copying contextual variables has certain performance penalty. To stop it at the certain level, just use `Promise.raw()` to undecorate derrived promises (as with any other decorator):
575577
```java

0 commit comments

Comments
 (0)