Skip to content

Commit 02f92e3

Browse files
committed
Adding docs for BlockingIO
1 parent 7a3c62b commit 02f92e3

File tree

1 file changed

+8
-6
lines changed

1 file changed

+8
-6
lines changed

README.md

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -349,9 +349,9 @@ for (int i = 0; i < SOME_LARGE_SIZE; i++) {
349349
}
350350
```
351351

352-
It's up to library user to select how often to check the flag: there are always trade-offs between overhead for regular execution time (when execution is not cancelled) and the delay before thread react on cancellation. In the example above the check may be moved from inner loop to the outer loop - this will speed-up normal execution path but the code will react on cancellation with delay.
352+
It's up to library user to select how often to check the flag: there are always trade-offs between overhead for regular execution time (when execution is not cancelled) and the delay before thread react on cancellation. In the example above the check may be moved from the inner loop to the outer loop - this will speed-up normal execution path but the code will react on the cancellation with delay.
353353

354-
Some API provides good built-in points for checks of early exits, the good example is [java.nio.file.FileVisitor](https://docs.oracle.com/javase/7/docs/api/java/nio/file/FileVisitor.html) + [java.nio.file.Files walkFileTree](https://docs.oracle.com/javase/7/docs/api/java/nio/file/Files.html#walkFileTree(java.nio.file.Path,%20java.nio.file.FileVisitor). Here you can just put checks in `previsitDirectory` / `visitFile`:
354+
Some API provides good built-in points to check for early exit, the good example is [java.nio.file.FileVisitor](https://docs.oracle.com/javase/7/docs/api/java/nio/file/FileVisitor.html) + [java.nio.file.Files walkFileTree](https://docs.oracle.com/javase/7/docs/api/java/nio/file/Files.html#walkFileTree(java.nio.file.Path,%20java.nio.file.FileVisitor). Here you can just put checks in `previsitDirectory` / `visitFile`:
355355

356356
```java
357357
@Test
@@ -363,7 +363,8 @@ public void testReallife() throws Exception {
363363
try {
364364
Files.walkFileTree(Paths.get("d:/"), new FileVisitor<>() {
365365
public FileVisitResult preVisitDirectory(Path dir, BasicFileAttributes attrs) throws IOException {
366-
System.out.printf("[%d ms] [%s] Task#%d: visiting dir %s%n", time.current(), Thread.currentThread(), id, dir);
366+
System.out.printf("[%d ms] [%s] Task#%d: visiting dir %s%n",
367+
time.current(), Thread.currentThread(), id, dir);
367368
if (Thread.currentThread().isInterrupted()) {
368369
// STOP when interrupted
369370
return FileVisitResult.TERMINATE;
@@ -385,7 +386,7 @@ public void testReallife() throws Exception {
385386

386387
2. You are using old blocking API of `java.io.InputStream` or `java.net.URLConnection`
387388
Probably, your code uses old blocking streams from `java.io` like `java.io.FileInputStream` or `SocketInputStream` opened from `java.net.Socket`. Or you are using `java.net.URLConnection`. Or outdated blocking Apache HTTP Client. These all prevents running thread from termination once interrupted -- all of these API-s are blocking and do not react on interruptions. The options are:
388-
- If you are using some third-party synchronous networking library then try to replace it with asynchronous version. For HTTP protocol there is a plenty of options like AsyncHttpClient and OkHttp. Both returns `CompletableFuture` as operation results and therefore could be easily plugged with the rest of Tascalate Concurrent code.
389+
- If you are using some third-party synchronous networking library then try to replace it with the asynchronous version. For HTTP protocol there is a plenty of options like AsyncHttpClient and OkHttp. Both returns `CompletableFuture` as operation results and therefore could be easily plugged with the rest of Tascalate Concurrent code.
389390
- If you are using blocking file API try to replace it with NIO version, it's the best option:
390391
391392
```java
@@ -400,7 +401,8 @@ import java.nio.file.Paths;
400401
import java.nio.file.StandardOpenOption;
401402
...
402403
try (InputStream in =
403-
Channels.newInputStream(FileChannel.open(Paths.get("E:/Downloads/LargeFile.rar"), StandardOpenOption.READ)))) {
404+
Channels.newInputStream(FileChannel.open(Paths.get("E:/Downloads/LargeFile.rar"),
405+
StandardOpenOption.READ)))) {
404406
... // Work with input stream
405407
}
406408
// Same could be done for sockets
@@ -423,7 +425,7 @@ Promise<?> promise = CompletableTask.supplyAsync(
423425
...
424426
promise.cancel(true);
425427
```
426-
`BlockingIO.interruptible(...)` can wrap as interruptible all the functional interfaces used in `CompletionStage` / `Promise` API. The parameter to the `BlockingIO.register` should be any implementation of `java.lang.Closeable` that will be closed on thread interruption. For `InputStream` or `OutputStream` is just the stream itself. For `java.net.HttpUrlConnection` you can use
428+
`BlockingIO.interruptible(...)` can wrap as interruptible all the functional interfaces used in `CompletionStage` / `Promise` API. The parameter to the `BlockingIO.register` should be any implementation of `java.lang.Closeable` that will be closed on thread interruption. For `InputStream` or `OutputStream` it is just the stream itself. For `java.net.HttpUrlConnection` you can use
427429

428430
```java
429431
HttpURLConnection conn = (HttpURLConnection)(new URL(url).openConnection());

0 commit comments

Comments
 (0)