Skip to main content

Concurrency in Go - Visualization


In this blog, let us discuss a program to visualize concurrency in Golang.
Reference and source:


Program 1:

import "fmt"
import "time"
func main() {
var Ball int
Ball = 5
table := make(chan int)
go player(table, 1)
go player(table, 2)
fmt.Println("mainstart:", Ball)
table <- Ball
time.Sleep(1* time.Second)
endres := <-table
fmt.Println("mainend:", endres)
}
func player(table chan int, playernumber int){
for{
ball := <-table
fmt.Println(playernumber,":",ball)
ball++
time.Sleep(100 * time.Millisecond)
table <- ball
}
}
/* ----------------------------------------------------------------------------------------------------------
Explanation:
Please try to visualize on your own before reading the explanation.


We will name the two goroutines that are called as player1_goroutine and player2_goroutine

Only when the chan table has some data, it can be read. Both the goroutines are initially waiting to read but the chan is empty, so when "table <- Ball" is executed, table has data and ball := <-table in player1_goroutine will read the data from chan.
      Now, player2_goroutine is waiting to read data from table and in the last line of the player method we write the ball value to table. Now, player2_goroutine will read the data from the channel.

This continues until the endres variable reads the data from the channel after a sleep time of 1 second.

That's why we can see player 1 and player 2 read data from the channel in an alternate manner and write it back till the channel read in the main method is invoked after sleep time.

 ---------------------------------------------------------------------------------------------------------- */

Comments

Popular posts from this blog

The one where Raj went for Cycling (First time Experience - 50km circuit)

The Past: I met a friend Srini in violin class and got to know that he goes for cycling with a bunch of explorers once in a month. I was excited and told him I had a cycle but haven't done cycling and would love to try. So he told me he will inform when they go for a ride. The Present:  Srini texts me that they were planning a morning ride of approx 50-60km circuit. Even though I was excited, I was a bit nervous if I could push myself for that far. I did think of it for a while and I was like "If not now, then when??? ". I said yes. It was a Sunday morning that we started off. The weather in Bangalore was a big threat as it was raining pretty consistently for the past fortnight. We trusted few weather reports which stated that there is not going to be any rain in the morning and there might be slight rain in the afternoon. Srini started from HSR, two of friends joined in Bellandur, I joined in Kadubeesanahalli junction and two more joined in ISRO junction. The r...

Python run watchdog along with a flash app

The pre-requisite for understanding this blog post would be the following post. Please go through the following to set the context to understand what is discussed in this post. https://rkinsideout.blogspot.in/2017/10/implementing-file-monitoring-script.html Problem : The flask app was running and was responding to a bunch of curl commands. This was single process thread that was working. When the watchdog api was integrated and initiated with the flask program, the problem was that the watchdog thread was started and the flask app was never started. It was started only when the watchdog thread ended. Since we want both the threads to be running, the watchdog process has to run in the background and the flask app to should run normally so that both functionalities are available for use. Python thread and threading are the options available. The "thread" starts a thread and waits for it to complete before proceeding with the rest of the flow to get execute...