Skip to content

Commit ddcc187

Browse files
authored
Add Usage in Java 8+
1 parent c41e267 commit ddcc187

File tree

1 file changed

+41
-1
lines changed

1 file changed

+41
-1
lines changed

README.md

Lines changed: 41 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ dependencies {
1616
}
1717
```
1818

19-
## Usage
19+
## Usage in Kotlin
2020
```kotlin
2121
ThreadSwitcher.newChain() // Create an empty chain.
2222
// Concat a UI chain to the empty chain.
@@ -47,6 +47,46 @@ ThreadSwitcher.newChain() // Create an empty chain.
4747
})
4848
```
4949

50+
## Usage in Java 8+
51+
```java
52+
ThreadSwitcher.newChain() // Create an empty chain.
53+
// Concat a UI chain to the empty chain.
54+
.onUI(v -> // v is Void type.
55+
{
56+
// Some tasks to do on a UI Thread
57+
return "result"; // Pass any value to the next chain.
58+
})
59+
// Concat a Worker chain to the previous chain.
60+
.onWorker((String result) -> // Receive the value from the previous chain.
61+
{
62+
// Some tasks to do on Worker Thread
63+
return Unit.INSTANCE; // Unit type of Kotlin
64+
})
65+
.onUI(unit -> // Unit type of Kotlin
66+
{
67+
// Some tasks to do on a UI Thread
68+
return Unit.INSTANCE;
69+
})
70+
.onWorker(unit ->
71+
{
72+
// Some tasks to do on a Worker Thread
73+
return "data";
74+
})
75+
76+
// Start to perform all chains from the top to bottom.
77+
.start(
78+
// This function would be invoked when all chains are finished without any exception.
79+
(String result) -> { // The type of parameter is settled by the return value of last chain.
80+
return Unit.INSTANCE;
81+
},
82+
// This function would be invoked when an exception occurred during performancing chains.
83+
(Throwable e) -> {
84+
return Unit.INSTANCE;
85+
}
86+
);
87+
```
88+
89+
5090
## License
5191

5292
```

0 commit comments

Comments
 (0)