Gordon  Matlala

Gordon Matlala


How to Synchronous and Asynchronous Fibers in ZIO

In this blog post, we will discuss operations on fiber both synchronous and asynchronous with the help of an example.


ZIO is a highly concurrent framework, powered by fibers, which are lightweight virtual threads that achieve massive scalability compared to threads, augmented with resource-safe cancellation, which powers many features in ZIO.

However, this powerful concurrency model lets you do more with less, achieving highly scalable, ultra-low-latency applications that are globally efficient and resource-safe.

ZIO is a next-generation framework for building cloud-native applications on the JVM, with a beginner-friendly yet powerful functional core.

ZIO library is purely functional and showcases the power of pure functional programming to solve modern business problems.


Fiber are lightweight equivalents of operating system threads. Similarly like a thread, a fiber models a running computation and instructions on a single fiber are executed sequentially.

However, fiber are much less costly to create than operating system threads, so we can have hundreds of thousands of fibers in our program at any given time whereas maintaining this number of threads would have a severe performance impact. Unlike threads, fiber are also safely interruptible and can be joined without blocking.

Synchronous Fibers

Here tasks are executed synchronously means one by one by a single fiber zio-default-async-1. Once a task is completed then only the execution of the new task will start on the same thread.

override def run(args: List[String]): URIO[zio.ZEnv, ExitCode] = synchronousRoutine().exitCode

val task1: UIO[String] = ZIO.succeed("Task-1")
val task2: UIO[String] = ZIO.succeed("Task-2")
val task3: UIO[String] = ZIO.succeed("Task-3")

def printThread = s"[${Thread.currentThread().getName}]"

def synchronousRoutine() = for {
  _ <- task1.debug(printThread)
  _ <- task2.debug(printThread)
  _ <- task3.debug(printThread)
} yield ()/**
 * Output:
 * [zio-default-async-1]: Task-1
 * [zio-default-async-1]: Task-2
 * [zio-default-async-1]: Task-3

Asynchronous Fibers

Here tasks are executing asynchronously means many at one time on different fibers:
zio-default-async-3, zio-default-async-4, zio-default-async-2 using fork method

override def run(args: List[String]): URIO[zio.ZEnv, ExitCode] = aSynchronousRoutine().exitCode

val task1: UIO[String] = ZIO.succeed("Task-1")
val task2: UIO[String] = ZIO.succeed("Task-2")
val task3: UIO[String] = ZIO.succeed("Task-3")

def printThread = s"[${Thread.currentThread().getName}]"

def aSynchronousRoutine() = for {
  _ <- task1.debug(printThread).fork
  _ <- task2.debug(printThread).fork
  _ <- task3.debug(printThread).fork
} yield ()/**
 * Output:
 * [zio-default-async-3]: Task-2
 * [zio-default-async-4]: Task-3
 * [zio-default-async-2]: Task-1
 * If you run this code, The output might not be exactly same as it is working asynchronously


In this blog, we discussed ZIO and ZIO fibers. We also took a brief about synchronous and asynchronous fibers in ZIO. To read more blogs like this, do check out here

You can also read about the basic Introduction to ZIO Fiber and fiber data type here

For exploring more regarding ZIO Fiber please refer to the following link


Original article source at: https://blog.knoldus.com/

#framework #jvm 

What is GEEK

Buddha Community

How to Synchronous and Asynchronous Fibers in ZIO
Heather  Schoen

Heather Schoen


Synchronous vs Asynchronous in Javascript

In this video, we’ll look at Synchronous vs Asynchronous in Javascript.

Follow me Instagram


#javascript #synchronous #asynchronous

Amber  Lubowitz

Amber Lubowitz


Database Replication - Synchronous vs Asynchronous - System Design Tutorials - Lecture 18

This is the eighteenth video in the series of System Design Primer Course. We talk about one more important component of System Design: Database Replication.
We want software engineers and aspiring software engineers to develop basics and get ready for the world of interviews as well as excelling as a Software Engineer.

#system design #database #synchronous #asynchronous

Synchronous vs Asynchronous Programming


In this article, we will discuss What Synchronous Programming is? What Asynchronous Programming is? And whether JavaScript is Synchronous or Asynchronous?

Many developers struggle to understand topics like Callbacks, Promises, and Async/Await, one of the reasons may be not understanding the actual need and core logic behind them.

Having a good grasp of this topic can help you understand them more easily.


This can be better explained with the help of an example.

Imagine you gave your friend a set of tasks:

  • Get Video Games from adjacent towns (somewhere far).
  • Get Chocolate from a nearby store.

What your friend now does is, he first completes task one and head to an adjacent town, and let’s say that takes x amount of time. Then he comes back to you, gives you video games, and then goes to perform the second task, and let’s say it takes y amount of time. The total time taken is x+y. This was just a couple of tasks but imagine there are hundreds of them, the total time is taken increases rapidly if he is a single person doing all the tasks one by one in the order they are given.

The scenario above discussed is how JavaScript runs its code by default, it goes line by line performing each task one at a time before moving on to another one, which means that for the last task or command in the code to run, all the code above it should be executed first. JavaScript’s code is executed in a single thread and if a function was to take some time to finish it would freeze everything else in the meantime. Executing one thing at a time is nothing but Synchronous.

By default,** JavaScript is a synchronous, blocking, single-threaded language**. This simply means only one operation will be in progress at a time. Windows alert function is a good example like,alert("Hello World") when this function is executed whole web page is stuck and you can’t interact with it until you dismiss the alert.

#javascript #async #async asynchronous #javascript developers #synchronous #programming

Carmen  Grimes

Carmen Grimes


Synchronization in Java: All You Need to Know

In this article, I’m going to discuss race conditions, why they occur, and finally how to handle it using a **synchronized**keyword.

Multi-threading is one of the essential skills for every java developer. I was quite interested before writing this article as I take this opportunity to share my thoughts and knowledge with you guys about Java synchronization.

Race Condition :

In a multi-threaded environment, when two or more threads try to update the mutable shared resource(data) at the same time, a race condition may occur because of context switching between the threads.

Race Condition example → Increment Operation:

1\. x = 100;
2\. for ( int i = 0; i < 10; i++ )
3\. {
4\.       x = x + 1; 
5\. }

In this example, if we run two threads **t1 **and **t2 **concurrently (in parallel) executing the above code, then the value of **x **may not be **120 **and the result would vary every time we run the code. This is happening because of thread context switching.

Let’s see what actually happens when the value of **x **is incremented.

Read the value of x
Add 1 to this value
Store this value to x

We can see that the increment is not an atomic step but it is happening in 3 steps.

These two threads can be at any step in this process at any time, and there can be context switching when a shared resource is involved. The state of x can be changed by another thread during the time between x is being read and when it is written back.

Let’s say a thread **t1 **retrieves the value of **x **but not yet incremented. Another thread **t2 **can also retrieve the same value of x (because thread t1has changed it yet) and then they would both be storing the same value (x+1) back in x.

#synchronization #programming #synchronized #java-programming #race-condition

Writing Synchronous Code in Swift

Turn asynchronous functions into synchronous, optimise your app flow and more.

When you write mobile apps in Swift, you usually have a lot of background work. I’ve been working as a mobile developer for almost 10 years and I can hardly remember a single project without Internet requests. Each Internet request requires time to be processed. Usually an unknown amount of time, possibly, endless.

If you do such work in the main or UI (which is the same) thread, your UI will get stuck. That’s why asynchronous tasks in Swift are designed the way they will never do so. The most common way is to avoid it is to use a callback or delegate. For example:

API.getUserInfo(userId: userId) { (userInfo, error) in

Here the app flow stops only for a small fraction of a second to prepare and send a request, but it doesn’t wait for the result. We get the result in a closure. userInfo contains information about a user (or nil if error happened), error is an optional error.

#ios-app-development #mobile-app-development #swift #asynchronous #synchronicity