Skip to content

Commit 111a703

Browse files
added new examples
1 parent a499322 commit 111a703

File tree

7 files changed

+147
-0
lines changed

7 files changed

+147
-0
lines changed

README.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
## Scala Concurrent Learn
2+
Learn about scala futures and promises, concurrent programming
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
package FuturesAndPromises
2+
3+
import java.util.concurrent.Executors
4+
import scala.concurrent.Future
5+
import scala.util.{Failure, Success}
6+
7+
/**
8+
* Example for creating single thread
9+
* */
10+
object ExampleExecutionContext {
11+
def main(args: Array[String]): Unit = {
12+
// defining single thread
13+
val executor = Executors.newSingleThreadExecutor()
14+
15+
implicit val ec = scala.concurrent.ExecutionContext.fromExecutorService(executor)
16+
17+
def simpleFuture(): Future[Int] =
18+
Future.successful(10)
19+
20+
simpleFuture onComplete {
21+
case Success(result) =>
22+
println(s"Result: ${result}")
23+
case Failure(e) =>
24+
println(s"error: ${e}")
25+
}
26+
27+
Thread.sleep(100)
28+
}
29+
}
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
package FuturesAndPromises
2+
3+
import scala.concurrent.ExecutionContext.Implicits.global
4+
import scala.concurrent.Future
5+
import scala.util.{Failure, Success}
6+
7+
/**
8+
* Recovering error message
9+
* */
10+
object ExampleFutureRecover {
11+
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")
14+
15+
checkIsBig(-1)
16+
.recover{ case error: IllegalStateException if error.getMessage == "number is negative" => 0 }
17+
.onComplete {
18+
case Success(result) =>
19+
println(s"Result: $result")
20+
case Failure(e) =>
21+
println(s"error: ${e}")
22+
}
23+
24+
// recoverWith,
25+
// Call Future.recoverWith to recover from a known exception
26+
checkIsBig(-1)
27+
.recoverWith{
28+
case error: IllegalStateException if error.getMessage == "number is negative" =>
29+
Future.successful(0)
30+
}
31+
.onComplete{
32+
case Success(result) =>
33+
println(s"Result: $result")
34+
case Failure(e) =>
35+
println(s"error: ${e}")
36+
}
37+
Thread.sleep(100)
38+
}
39+
}
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
package FuturesAndPromises
2+
3+
import scala.concurrent.ExecutionContext.Implicits.global
4+
import scala.concurrent.Future
5+
6+
/**
7+
* andThen() is used,
8+
* whenever you have a need to apply a side-effect function on the result returned by the future
9+
* */
10+
object ExampleFutureThen {
11+
def main(args: Array[String]): Unit = {
12+
def check(item: String): Future[Int] =
13+
if (item.length > 0) Future.successful(item.length) else Future.successful(0)
14+
15+
val futureThen = check("key")
16+
17+
// Call Future.andThen with a PartialFunction
18+
futureThen.andThen {
19+
case result =>
20+
println(s"Result: ${result}")
21+
}
22+
23+
Thread.sleep(100)
24+
}
25+
}
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
package FuturesAndPromises
2+
3+
import scala.concurrent.ExecutionContext.Implicits.global
4+
import scala.concurrent.Future
5+
import scala.util.{Failure, Success}
6+
7+
/**
8+
* The Future.zip will create a new future,
9+
* whose return type will be a tuple holding the return types of the two futures
10+
* */
11+
object ExampleFutureZip {
12+
def main(args: Array[String]): Unit = {
13+
def item(): Future[String] = Future.successful("apple")
14+
def price(): Future[Double] = Future.successful(9.99)
15+
16+
// zip item with price
17+
val zippedFuture = item zip price
18+
19+
zippedFuture onComplete {
20+
case Success(result) =>
21+
println(s"Result: ${result}")
22+
case Failure(e) =>
23+
println(s"Error: ${e}")
24+
}
25+
26+
// zipWith, difference is that,
27+
// the zipWith() method allows you to pass-through a function which can be applied to the results
28+
def itemWith(i: String): Future[Option[String]] = {
29+
Future(
30+
Some(i)
31+
)
32+
}
33+
34+
val future: (Option[String], Double) => (String, Double) = (some, price) => (some.getOrElse(""), price)
35+
val futureZipWith = itemWith("apple-1").zipWith(price())(future)
36+
37+
futureZipWith onComplete {
38+
case Success(result) =>
39+
println(s"Result: ${result}")
40+
case Failure(e) =>
41+
println(s"Error: ${e}")
42+
}
43+
44+
Thread.sleep(100)
45+
}
46+
}

src/main/scala/FuturesAndPromises/ScalaFuture.scala

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,5 +39,11 @@ object ScalaFuture {
3939
println("Handling Future with map")
4040
println(s"Sum: ${answer}")
4141
}
42+
43+
// print with foreach
44+
add(7, 10).foreach{ answer =>
45+
println("Handling Future with foreach")
46+
println(s"Sum: ${answer}")
47+
}
4248
}
4349
}
243 Bytes
Binary file not shown.

0 commit comments

Comments
 (0)