Skip to content

Commit 6cb5574

Browse files
added new examples
1 parent 1ad42a8 commit 6cb5574

File tree

2 files changed

+78
-8
lines changed

2 files changed

+78
-8
lines changed

README.md

Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,70 @@
11
## Scala Concurrent Learn
22
Learn about scala futures and promises, concurrent programming
3+
4+
#### Scala Future Basics
5+
6+
Learn about Futures and how to print its results, Feature is used for read. [example code](src/main/scala/FuturesAndPromises/ScalaFuture.scala)
7+
8+
#### Scala Promise
9+
10+
Promise can be written (normally only once), Promise is used for write.
11+
[example code](src/main/scala/FuturesAndPromises/ScalaPromise.scala)
12+
13+
#### Scala Concurrent example
14+
15+
How to do calculation concurrently with Features. [example code](src/main/scala/FuturesAndPromises/ExampleConcurrent.scala)
16+
17+
#### Scala Synchronous example
18+
19+
How to do calculation synchronously with Features, used Await.result to wait result. [example code](src/main/scala/FuturesAndPromises/ExampleSynchronous.scala)
20+
21+
#### Scala Parallel calculation example
22+
23+
How to do calculation parallel with Features, used Future.sequence.
24+
[example code](src/main/scala/FuturesAndPromises/ExampleParallel.scala)
25+
26+
#### Scala Parallel-2 calculation example
27+
28+
How to do calculation parallel with Features, used Future.traverse, traverse and sequence are similar, difference is that traverse
29+
easily convert all the Option[Int] into just Int type.
30+
[example code](src/main/scala/FuturesAndPromises/ExampleParallel2.scala)
31+
32+
#### Scala Zip two features example
33+
34+
The Future.zip will create a new future, whose return type will be a tuple holding the return types of the two futures.
35+
[example code-1](src/main/scala/FuturesAndPromises/ExampleFutureZip.scala), [example dode-2](src/main/scala/FuturesAndPromises/ExampleReadingFile.scala)
36+
37+
#### Scala FoldLeft example
38+
39+
The foldLeft on your future operations will be run asynchronously from left to right.
40+
[example code](src/main/scala/FuturesAndPromises/ExampleFoldLeft.scala)
41+
42+
#### Scala ReduceLeft example
43+
44+
The reduceLeft is similar with foldLeft, however, reduceLeft() does not allow you to provide a default value.
45+
[example code](src/main/scala/FuturesAndPromises/ExampleReduceLeft.scala)
46+
47+
#### Scala FutureThen example
48+
49+
The andThen is used for passing partialFunction.
50+
[example code](src/main/scala/FuturesAndPromises/ExampleFutureThen.scala)
51+
52+
#### Scala FirstCompletedOf callback function example
53+
54+
The firstCompletedOf return any one of the futures which finish first.
55+
[example code](src/main/scala/FuturesAndPromises/ExampleFirstCompletedOf.scala)
56+
57+
#### Scala ExecutionContext example
58+
59+
In this example, learn how to create single threaded executionContext.
60+
[example code](src/main/scala/FuturesAndPromises/ExampleExecutionContext.scala)
61+
62+
#### Scala Future recover example
63+
64+
The recover function recovers error message.
65+
[example code](src/main/scala/FuturesAndPromises/ExampleFutureRecover.scala)
66+
67+
#### More resources for concurrent and parallel programming in Scala
68+
- [Scala Futures for Beginners](http://allaboutscala.com/tutorials/chapter-9-beginner-tutorial-using-scala-futures/#futures-introduction)
69+
- [Monix](https://monix.io/)
70+
- [Akka Actors](https://doc.akka.io/docs/akka/current/typed/actors.html#:~:text=SayHello(%22Akka%22))%3B,running%20many%20actors%2C%20per%20JVM.)

src/main/scala/FuturesAndPromises/ExampleFutureRecover.scala

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -9,31 +9,33 @@ import scala.util.{Failure, Success}
99
* */
1010
object ExampleFutureRecover {
1111
def main(args: Array[String]): Unit = {
12-
def checkIsBig(number: Int): Future[Int] =
13-
if (number > 0) Future.successful(10) else throw new IllegalStateException("number is negative")
12+
13+
def checkIsBig(number: Int): Future[Int] = Future {
14+
if(number > 0) number
15+
else throw new IllegalStateException("number is negative")
16+
}
1417

1518
checkIsBig(-1)
16-
.recover{ case error: IllegalStateException if error.getMessage == "number is negative" => 0 }
19+
.recover { case e: IllegalStateException if e.getMessage == "number is negative" => 0 }
1720
.onComplete {
18-
case Success(result) =>
19-
println(s"Result: $result")
20-
case Failure(e) =>
21-
println(s"error: ${e}")
21+
case Success(donutStock) => println(s"Results $donutStock")
22+
case Failure(e) => println(s"Error processing future operations, error = ${e.getMessage}")
2223
}
2324

2425
// recoverWith,
2526
// Call Future.recoverWith to recover from a known exception
2627
checkIsBig(-1)
2728
.recoverWith{
2829
case error: IllegalStateException if error.getMessage == "number is negative" =>
29-
Future.successful(0)
30+
Future.successful(1)
3031
}
3132
.onComplete{
3233
case Success(result) =>
3334
println(s"Result: $result")
3435
case Failure(e) =>
3536
println(s"error: ${e}")
3637
}
38+
3739
Thread.sleep(100)
3840
}
3941
}

0 commit comments

Comments
 (0)