Trending March 2024 # How Does Wait Function Work In Lua # Suggested April 2024 # Top 9 Popular

You are reading the article How Does Wait Function Work In Lua updated in March 2024 on the website Cattuongwedding.com. We hope that the information we have shared is helpful to you. If you find the content interesting and meaningful, please share it with your friends and continue to follow and support us for the latest updates. Suggested April 2024 How Does Wait Function Work In Lua

Introduction to Lua wait.

The Lua wait is a function used when multiple processes simultaneously and want the primary process to stop for a given time. It is a method useful for stopping or delays the parent process until the child process is not working completely. It helps to reduce complications of the multiple processes at a time and work orderly. This function helps to stop or sleep parent thread for a minute, second, and millisecond and child thread within time. It helps to stop the parent thread for the child thread procedure and start the parent thread process after finish the child thread process. This method gives sleep time to work multiple thread procedures together without any critical issues.

Start Your Free Software Development Course

Web development, programming languages, Software testing & others

Syntax of Lua wait

Given below is the syntax of Lua wait:

function wait(unit of time) Lua source codes for wait function… end wait(required time)

Explanation:

The “wait (unit of time)” is a method used to give the unit of time like a minute, second, and millisecond to stop the working procedure.

Example:

Code:

function wait ( millisecond ) end

The source code assigns the time to thread procedure for multiple operations.

The wait function delays the time in the millisecond using syntax example.

The wait (required time) is a function using forgiven time as per the user’s requirements.

Example:

wait( 1 )

The wait function is to stop the time to parent thread for one (1) millisecond.

How does wait Function Work in Lua?

Step 1: Lua programming IDE Environmental Setup.

The Lua text editor, Lua compiler, and Lua interpreter install in your computer as per the operating system and software version.

Or

It does not have software; then, you can use Lua Online IDEs for coding and start Lua programming.

Step 2: Create the Lua File.

The Lua file creates with the .lua extension and writes a source code.

Step 3: Creates the wait function with the time unit argument.

Code:

function wait(millisecond) write a code for the Lua wait function… end

Step 4: Create a variable with the operating system time.

Code:

local ostime_vrbl = os.time()

Step 5: Print the running time of the system.

print( "timing of Lua wait function: ", ostime_vrbl );

Step 6: Create a Lua wait method with the given time.

Code:

wait(0.5);

Combine the procedure of the Lua waits for demo example.

Code:

function wait(millisecond) local ostime_vrbl = os.time() print( "timing of Lua wait function: ", ostime_vrbl ); end wait(0.5); Examples of Lua wait

Given below are the examples mentioned:

Example #1

The Lua wait function with millisecond example and output.

Code:

function wait(millisecond) local ostime_vrbl = os.time() + millisecond print( "timing of Lua wait function: ", ostime_vrbl ); end wait(0.5); wait(1); wait(1.5); wait(2); wait(2.5); wait(3);

Output:

The example is using a millisecond unit of time for function.

Example #2

Function with the second example and output.

Code:

function wait(second) local ostime_vrbl = os.time() + second print( "timing of Lua wait function: ", ostime_vrbl ); end wait(1); wait(1.5); wait(2); wait(3); wait(4); wait(5);

Output:

Explanation:

The example is using the second unit of time for function.

Example #3

Function with millisecond and second Example and output.

Code:

function wait(second, millisecond) local ostime_vrbl = os.time() + second, millisecond print( "timing of Lua wait function: ", ostime_vrbl ); end wait(1, 2); wait(1, 0.5); wait(2, 1); wait(3, 0.7); wait(4); wait(5, 0);

Output:

Explanation:

The example is using the second and millisecond unit of time for function.

If we use two units of time, then the user either uses both time units or a single time unit.

Example #4

Lua wait function with “do while” condition example and output.

Code:

function wait(second, millisecond) local ostime_vrbl = os.time() + second, millisecond; while os.time() < ostime_vrbl do end print( "timing of Lua wait function: ", ostime_vrbl ); end wait(1, 2); wait(1, 0.5); wait(2, 1); wait(3, 0.7); wait(4); wait(5, 0);

Output:

Explanation:

The example is using the second and millisecond unit of time for the Lua wait function.

The Lua wait function is using the “do while” condition for comparison time.

If the operating system time is less than the given time, then the wait function works.

Example #5

Code:

function wait(second, millisecond) local ostime_vrbl = os.time() +  millisecond; repeat until os.time() < ostime_vrbl print("timing of Lua wait function: ", ostime_vrbl); end wait(1, 2); wait(1, 0.5); wait(2, 1); wait(3, 0.7); wait(4, 1); wait(5, 0);

Output:

Explanation:

The example is using the second and millisecond unit of time for the Lua wait function.

It is used milliseconds to return the time value.

This function is using the “repeat” condition for comparison time.

If the operating system time is less than the given time, then the wait function works.

Example #6

Function with compares time and variable example and output.

Code:

function wait(second, millisecond) local ostime_vrbl = os.time() + second, millisecond; print( "timing of Lua wait function: ", ostime_vrbl ); end wait(1, 2); wait(1, 0.5); wait(2, 1); wait(3, 0.7); wait(4); wait(5, 0);

Output:

Explanation:

The example is using the second and millisecond unit of time for the Lua wait function.

The Lua wait function is using the “do while” condition for comparison time.

If the operating system time is greater than the given time, then the wait function works.

Example #7

Lua waits with zero second example and output.

Code:

function wait(second, millisecond) local ostime_vrbl = os.time() + second, millisecond; while os.time() < ostime_vrbl do end print( "timing of Lua wait function: ", ostime_vrbl ); end wait(0);

Output:

Explanation:

The example is using the second and millisecond unit of time for the function.

The wait method does not use empty or nil values until an error occurred.

The user must give zero (0), millisecond, second, or minute inside for the wait method.

Conclusion

The Lua wait function helps to avoid complications of multiple operations. This function makes easy, elegant, and sophisticated applications. This function makes a fast, error-free application.

Recommended Articles

We hope that this EDUCBA information on “Lua wait” was beneficial to you. You can view EDUCBA’s recommended articles for more information.

You're reading How Does Wait Function Work In Lua

How Does Subroutine Function Work In Perl?

Introduction to Perl Subroutine

Perl Subroutine is used to reusable code using a subroutine that we can use code, again and again, there is no need to repeatedly write the code. We can create our own functions it is called a subroutine. Subroutines are very important to improve the code reusability. It is also used to improve the code readability, using subroutine we can improve the code readability of our program. To use subroutine in our program we need to define or need to create it first, after creating then we call a subroutine in our code.

Start Your Free Software Development Course

Web development, programming languages, Software testing & others

Syntax of Perl Subroutine

1. Defining a subroutine: To use subroutine in our program we need to create it first and then we need to call in our program. Below syntax shows define or create a subroutine in perl are as follows.

Syntax:

Sub subroutine_name  -- Name of subroutine that we have defining. { Statements;    -- Statement to be used in body of the subroutine. Return; }

2. Calling Subroutine: In perl we calling subroutine by passing a list of arguments. Below syntax shows calling a subroutine in perl are as follows.

Syntax:

subroutine_name (arguments_list); -- List of arguments which was used with subroutine. Parameters of Perl Subroutine

Below is the parameter description syntax of the subroutine in Perl is as follows.

Subroutine Name: It is very important while creating subroutine. We have defined any name to subroutine while creating the same. The subroutine name is the name that we have defined to the subroutine.

Statement: The statement is defined as a block or body of the code of subroutine. This code is written under the body of the subroutine. We can define one or more statement in the body of the subroutines.

Return: This is defined as the return an argument to its calling functions or subroutines. We can return no of arguments to the calling function in perl.

Argument List: This is defined as arguments which we have used at the time of calling a subroutine in Perl. We have pass one or more arguments at the time of calling a subroutine in Perl.

How Does Subroutine Function work in Perl?

Below is the working of the subroutine is as follows. Below is the important use of subroutine in perl.

Reusability of code.

Improve the code readability.

The subroutine is used to improve code reusability. If we want to take input from the user multiple times at the same time we have creating subroutine and then we call the subroutine in our program whenever we need it.

We can improve the code reusability while using the subroutine in our program. After using subroutine there is no need to write it again and again.

We can create our own functions it is called subroutines.

To use subroutine in our program we need to define or need to create it first, after creating then we call the same in our code.

It is used to reusability of code using subroutine we can use code, again and again, there is no need to write the code again and again.

It is also used to improve the code readability, using subroutine in perl we can improve the code readability of our program.

Subroutines is very important and useful to improve code reusability and readability.

It or function is a group or statement that used together to perform a task. We can divide our code into separate subroutines.

To divide subroutine into separate subroutines is depends on the developer but we can divide it logically based on the function which performed a specific task.

In the subroutine, function and method are the same but in other languages this is different.

The subroutines are used in perl programming language because subroutine in Perl created by using sub keyword.

When we have called to function or subroutines in our program, perl compiler will stop its all executing program and goes to execute the function to execute the same and then return back to execute the remaining section code.

We have avoided this by using the return statement in our program.

Examples

Below is the example of the subroutine in Perl is as follows.

Example #1

Passing Arguments to Subroutine: Below example shows passing arguments to a subroutine.

Code:

# Defining function in perl. sub Average { # Dispay number of arguments. $number = scalar(@_); $sum_of_num = 0; foreach $item (@_) { $sum_of_num += $item; } $average_of_num = $sum_of_num / $number; print "Average of numbers : $average_of_numn"; } # Calling function average to define average of given numbers. Average(100, 200, 300, 400, 500);

Example #2

Passing List to Subroutine: Below example shows passing a list to a subroutine.

Code :

# Defining function in perl with list as arguments. sub sub_PrintList { my @sub_list = @_; print "Given list @sub_listn"; } $p = 1; @q = (2, 3, 4, 5, 6, 7, 8, 9, 10);  ## Provide list as user input. # Function call with list parameter sub_PrintList($p, @q);

Output:

Example #3

Passing Hashes to Subroutine: Below example shows passing a hashes to a subroutine. In the below example, we have to pass the hashes to the subroutine.

Code:

# Defining function in perl with hashes as arguments. sub sub_PrintHash { my (%sub_hash) = @_; foreach my $key ( keys %sub_hash ) { my $hash_value = $sub_hash{$key}; print "$key : $hash_valuen"; } } # Function call with hashes parameter sub_PrintHash(%sub_hash);

Example #4

Returning Value from a Subroutine: The below example shows returning a value from a subroutine. In the below example, we have to return value from a subroutine.

Code:

# Defining function in perl. sub Average { $number = scalar(@_); $sum_number = 0; foreach $item (@_) { $sum_number += $item; } $average_num = $sum_number / $number; return $average_num; } # Function call $num = Average(10, 20, 30, 40, 50, 60, 70, 80, 90, 100); print "Average for numbers : $numn";

Output:

Conclusion

In perl we can create our own functions it is called as subroutines, subroutines is very important to improve the code reusability. It is used to reusability of code using subroutine we can use code again and again, there is no need to write the code again.

Recommended Articles

We hope that this EDUCBA information on “Perl Subroutine” was beneficial to you. You can view EDUCBA’s recommended articles for more information.

How Does Collect Function Work In Scala With Examples

Introduction to Scala collect.

Collect function is used to collect elements from the given collection. Collect function can be used with the collection data structure to pick up some elements which satisfy the given condition. Collect function can be used with the mutable and immutable collection data structure in scala. It always returns us the new collection containing all the elements that satisfy the condition; this condition is referred to as the Partial function. That means it took the partial function and applied it to the all the values present inside the given collection.

Start Your Free Software Development Course

Web development, programming languages, Software testing & others

We know that it is a function available in Scala, which takes up the partial function as the parameter and will be applied on all the variables present inside it and return us the elements that satisfy the condition.

Let’s see the syntax of this function.

This is the syntax as per the scala doc:

def collect[B](pf: PartialFunction[A, B]): Traversable[B] mylistName.collect(Your_partial_function)

As you can see in the above lines of code, we can call this function on our collection object by specifying a partial function inside it as the parameter. So after calculating the values, it will return us a new collection with result values inside it.

How does collect Function Work in Scala?

We know that the collect function in scala to collect the element from a collection that are of the same type. We can call this function on any collection type, whether it is mutable or immutable. It will always return us the new collection as a result of the given collection element.

Method signature:

def collect[B](pf: PartialFunction[A, B]): Traversable[B] (This is as per the scala documentation)

1. Partial Function

The collect function takes a partial function as the parameter. This partial function will be responsible for performing the operation on the each element of the collection. It will pick out all the elements from the given collection that will satisfies the partial function.

Syntax:

In the above lines of code, we can define a partial function, what it will take and what it is going to return. This is simple to use and readable also.

val demo: PartialFunction[String, String] = { }

In the above lines of code, we are creating one partial function which will take up the string parameter and return us the string. In this way, we can specify our type what we want from the given collection.

2. Return Type

Collect function will always return us the new collection, which will contain all the elements from the given collection data structure.

Example:

object Main extends App{ val mysequence: Seq[Any] = Seq("hello", "hello again", 40, "bye", 60, 80, 100, "i am strinf as well !!") }

It is just a simple program in which we are creating a sequence of ‘any’ type in scala; also, we are specifying the different type of element inside it, for instance, string and integer. But we want to extract only the string elements from the collection, so we call the collect method and pass our partial function to match only the string. In this way, we can use a collect function in scala.

Points to be remembered while using a collect function in scala:

This can be used with the mutable and immutable collection data structure.

This function takes a partial function as the argument; this function will calculate and pick up the elements which satisfies the given condition. Always it will return as a result in the form of a new collection contain all the element.

Examples of Scala collect.

Given below are the examples of Scala collect:

Example #1

In this example, we are creating a sequence collection of any type. Using the collect function, we are trying to extract the integer, string, and double values from the given collection data structure in the scala.

object Main extends App{ println("Demo to understand collect function in scala !!") println("Extrat only string values ::") val mysequence1: Seq[Any] = Seq("hello", "hello again", 40, "bye", 60, 80, 100, "i am string as well !!") println("Result is ::") println(result1) println("***********************************************************************") println("Extrat only integer values ::") val mysequence2: Seq[Any] = Seq("Amit", 200, 20.1, "sumit", 300, 30.2, "Geet", 400 , 40.1, "vinit", 500, 50.1) println("Result is ::") println(result2) println("***********************************************************************") println("Extrat only double values ::") val mysequence3: Seq[Any] = Seq("Amit", 200, 20.1, "sumit", 300, 30.2, "Geet", 400 , 40.1, "vinit", 500, 50.1) println("Result is ::") println(result3) }

Output:

Example #2

In this example, we are trying to fetch the float value from the collect data structure. Here also we are using the collect function to extract the element from the collection. Also, we are not using any library; this is an in build function available in scala.

Code:

object Main extends App{ println("Demo to understand collect function in scala !!") println("Extrat only float values ::") val mysequence1: Seq[Any] = Seq(2.1f, "test 1", "test2", 5.7f, "test 3", "test 4", "tets 5", 8890, 1.0f, 8.9f, 2.4f) println("Result is ::") println(result1) println("***********************************************************************") }

Output:

Conclusion

By using the collect function, we can easily extract the data from the collection, which are of the same type. Also, we can use this function with any mutable and immutable collection data structure, which makes it more easy and more handy to use.

Recommended Articles

We hope that this EDUCBA information on “Scala collect” was beneficial to you. You can view EDUCBA’s recommended articles for more information.

How Wait Method Work In Powershell? (Examples)

Definition of PowerShell Wait

PowerShell wait cmdlets are used to hold the execution for some certain period to wait for the command, job, or Process to complete, to notify a user that background job is running and ask to wait for some specific period, to restarting remote systems, and wait until it comes up, starting and stopping VMs, etc, and that depends on the command specified. If the command is for the background Job to process first then it will wait to finish and if the command is to halt the execution for a specific period then the user has to wait until the time specified.

Start Your Free Data Science Course

Hadoop, Data Science, Statistics & others

Syntax:

a. Wait-Process

The above command is used to wait for the processes to be stopped before accepting the new input or moving to the next commands.

b.Wait-Job

This command is used to complete the background job and execution won’t move to the next stage until the execution is finished.

c. Start -Sleep

This command halts the script execution for the specified time in seconds or milliseconds and can’t make any command or job to wait in the background but pauses the script execution.

This command suspends the execution of the script or function until the particular event is raised and continues the execution of the script after that.

e. Wait Parameters

Many commands use the -Wait parameter directly in the command to wait for that command to finish instead of using the Wait-Job command running separately.

f. Timeout

Similar to the Start-Sleep command, this command holds the execution of the script for the specified period.

How Wait Method works in PowerShell?

Once we use the Wait commands, it stops the execution until the background job or process completes and resumes the execution from the last point where it had stopped. Some commands like Start-Sleep and timeout, just pause the script but don’t run any job or process in the background.

Examples

Let us discuss examples of PowerShell Wait.

1. Wait-Process Example

When we use the command Wait-Process, it waits for the particular process or application to be stopped. This command accepts the Process ID, Name, and the Timeout in seconds.

For example, we have the SnippingTools application running and we are going to terminate it but we will wait until the process is terminated.

Get-Process SnippingTool

To use the wait command we can either use ProcessID or the Name of the process.

Write-Output ”This command will be executed after process termination”

You can also provide the timeout parameter in seconds if the process doesn’t terminate in the specified time, it will throw an error.

You can also provide the multiple process names for the Wait-Process command.

2. Start-Sleep Example

This command holds the execution for a certain amount of time and the next command waits until then. You can provide time in Seconds or milliseconds.

Write-Output "Writing First Line"

Start-Sleep -Seconds 5

Write-Output “This command will be executed after 5 seconds”

You can also provide the -milliseconds parameter.

Start-Sleep -Milliseconds 5000

c. Timeout Example

This is a cmd command but can be accessible in PowerShell as well. Once you specify the Timeout in seconds, the next commands wait until the timeout.

Write-Output “This command will be executed after 5 seconds”

Output:

Users can interrupt in the timeout specified if you want uninterrupted Timeout then use /NoBreak switch.

The only option with the /NoBreak switch is to press Ctrl+C to terminate further execution.

d. Wait-Job Example

When we use the Wait-Job command, it waits for the scriptblock to complete and executes the next line command.

Write-Output ”This line will be executed after job completes”

Output:

We can also specify the timeout for the job to complete using the -Timeout parameter in seconds in the Wait-Job cmdlet. If the job doesn’t complete during that period, the script will throw an error.

e. Wait-Event Example

This command waits for the particular event to be raised and PowerShell suspends the execution until then and resumes when the event triggers.

Wait-Event

The above command waits for any next event to trigger and suspends the PowerShell execution for that time.

We will create a new test event here to trigger the Wait-Event command.

Output:

If we have any particular source for that event, we can specify that SourceIdentifier Name.

Wait-Event -SourceIdentifier "ProcessStarted" -Timeout 200

g. -Wait Parameter in Commands

Few commands like Restart-Computer, Start-AZVM, Stop-AzVM, etc support the -Wait parameter and the next commands execute after executing the -Wait parameter command. For example,

Get-Process -ComputerName test1-win2k12

Output:

There are other commands which by default include the Wait Parameter like Start-AzVM and Stop-AzVM command.

Write-Output ”This command will be executed immediately”

To Stop VM,

Write-Output ”This command will be executed after VM Stops”

In the first command Start-AZVM, the -NoWait parameter is specified so that command won’t wait to start the VM but in the second command Stop-AZVM, the default parameter is Wait so that the next command will wait until the VM stops successfully.

Conclusion

There are many Wait cmdlets in PowerShell and supported Wait parameter in PowerShell commands so that the previous commands should complete first because sometimes the next command input depends on the output of the previous command and we need them to be executed first.

Recommended Articles

This is a guide to PowerShell Wait. Here we discuss the Definition, How Wait Method works in PowerShell and examples with code implementation. You may also have a look at the following articles to learn more –

How Sort() Algorithm Function Work In C++

Introduction to C++ sort()

The sort() function in C++ sorts a number of elements or a list of elements within the first to last elements in an ascending or descending order. This function executes the sorting operation within the specified list or range, starting with the first element and ending with the last element. The sort function takes in two parameters as arguments and returns the result of sorting. Comparing the elements is the main operation. There can be exceptions in case the elements that are being compared encounter any exceptions. The most common ways to sort are in either an ascending order or a descending order.

Start Your Free Software Development Course

Syntax and parameters:

The standard syntax for the sort function includes of sort keyword and two parameters. The return output will be the result of the sort operation.

void sort(RIt first, RIt last);

The parameters implemented in the above syntax are RandomIt, first and last.

Here the first and last are the range between whose sorting is to be done; the first notifies the first element of the list, while the last denotes the last element.

How sort() Algorithm Function work in C++?

The basic method by which the sorting algorithm works is based on comparison.

The sorting function attempts to compare each and every element of the list.

The comparison works in a way as comparing the first element with the second element, followed by the second and third, and so on. Here the comparison operator “<” is widely used.

So, simply speaking, the sort function at a time picks two values or elements of a list, then compare these two values to identify smaller and bigger value and arrange them in a form, ascending or descending, whichever is required.

Examples of C++ sort()

We will have a list of values, which will be unsorted and not in any order, and we will aim to correct the list by implementing the sort function.

Example #1

Our first example takes in an array of number values, unsorted, and we will implement the sort, and the output will be a list of values in a sorted format.

Code:

using namespace std; int main() { int arr1[] = {99,91,94,96}; int n = sizeof(arr1)/sizeof(arr1[0]); sort(arr1, arr1+n); cout << “n List of Array after sorting is: “; for (int i = 0; i < n; ++i) cout << arr1[i] << ” “; return 0; }

Explanation:

We have our system files and namespace, followed by initializing the main code. Then we have our first array variable of integer type, which holds a list of four numbers totally unsorted, following no order.

Then we have another integer variable followed by the sort function. We then print s statement, which then comes in our for statement, which will pick and print the numbers in a sorted format.

Upon successful execution, the output of the program will be a statement and a list of four numbers in a sorted format.

Output:

As explained and expected, the program generates an output statement followed by the corrected and sorted list of numbers. The sorting operation is performed in ascending order.

Example #2

Code:

using namespace std; void show(int a[]) { for(int b = 0; b < 10; ++b) cout << a[b] << ” “; } int main() { int a[10]= {3,4,1,0,5,7,8,6,9}; cout << “n This is the unsorted list: “; show(a); sort(a, a+10); cout << “nn Here’s the output of sort operation:: “; show(a); return 0; }

Explanation:

Started with required system files followed by declaring the login for the program. Then we have our main block of code, where we have an array with integer values followed by a print statement.

Then we have our sort function and parameter values passed, followed by the print statement and the final output.

The expected output is two statements, which include a list of the unsorted array and another list which is the output of the sorting operation.

Output:

Our code executed as expected, and the output is proper.

Example #3

Code:

int main() { std::cout << ‘n’; std::cout << “Sorting done with simple < operator: “; std::cout << ‘n’; std::sort(list.begin(), list.end()); for (auto a : list) { std::cout << a << “  “; } std::cout << ‘n’; std::cout << “n This is output of sorting using custom function: “; std::cout << ‘n’; struct { bool operator()(int a, int b) const { return a < b; } } customLess; std::sort(list.begin(), list.end(), customLess); for (auto a : list) { std::cout << a << ” “; } std::cout << ‘n’; }

Explanation:

Like the earlier example, we begin with the required system files and a main block. Then we have our array of values, followed by a few lines of code for output, where we first print the sorted list as a result of our sort operation. Then within our second method, we have a custom function, which creates a struct and passes the values.

Then we have our sort function, which starts with begin, then the end, and the custom function.

Output:

As expected, the output of the code is that the sorting has been done in two different formats using two methods.

Conclusion

In C++, we use the sort() function to sort a list of values. This function allows us to perform sorting in either ascending or descending order. We saw a few methods and ways to implement the sort function.

Recommended Articles

This is a guide to C++ sort(). Here we discuss the introduction to C++ sort(), how sort() algorithm function works, along with programming examples. You may also have a look at the following articles to learn more –

How Does Concurrency Work In Go Language?

Introduction to Golang Concurrency

Web development, programming languages, Software testing & others

Syntax of Golang Concurrency

Below is a simple syntax for the concurrency of the go language, We can explain the below go, here in the below first we have written a function with the name name-of-function and this function contains several expressions. These expressions can be any calculation and some other tasks. Now suppose we wanted to run expression 1, expression 2, and expression 3 at the same time we want to execute the expression, or very simply we want to concurrently execute them. So we can use the sleep and this will allow us to control execute them at the same time.

Please see the below syntax for a better understanding.

func name-of-function(){ } go name-of-function() How Does Concurrency Work in Go language?

Before going to discuss the working of the concurrency in go language let us understand the basic concept and uses of the concurrency in the Go language. You have seen YouTube video, if you will see there are always two things that will be happening at one time one is the playing of the video and another is the buffering of the video. This means even you pause the video buffering happens, and after that even inter gone which means in offline mode also you will be able to play the video. This all happening because of the concurrency, which is allowing the application to run at one time, and at the same time, it allows us to buffer the video for capturing the video for offline mode. All these things are happening just because of the concurrency. So basically in concurrency first one particular task will run and in the meanwhile, sleep function will be called which will give space to run another task. We have already discussed the goroutine, basically, it is a call between two goroutines, go language there will be multi goroutine and there will be one main goroutine, every goroutine will fall under the main goroutine and if the main goroutine will terminate then other all go routine all be terminated. We can discuss point to point to the working of the concurrency of the.

Concurrency allows you to use goroutine which is blockers, you know once a go routine gets called it will block for execution of others.

Hence with the help of sleep, we will be able to call concurrent goroutine.

Every goroutine belongs to the main goroutine in go language and if the main goroutine will be terminated then all other goroutines will also terminate.

Examples to Implement Golang Concurrency

In the below we have given three examples, these examples explain the way of working of the concurrency. If we wanted to execute this code then we can take the example and pass them on any file and run the command go run chúng tôi and we can see the output.

Example #1

Below is a simple example where we are calling a function show and this call continuously prints the data, but here we are not printing data in the concurrency, which means we are printing one by one.

Code:

package main import "fmt" func show(name string) { for i := 0; i< 6; i++ { fmt.Println(name) } } func main() { go show("Ajay") show("Ranjan") }

Example #2

In the below example we have a function called the show and that function is defined with the help of the go keyword and then this show function called, inside the function definition we have written the logic to print the param passed to the function show and each time when it get called it will be print the param. Now the question is we want to execute then concurrently which means together execution of these two go show(“Ajay”) and show(“Ranjan”). So we have written a sleep function inside the show definition, it will allow two simultaneous calls to the function for executions. See the example output first it is printing Ajay and then it printing Ranjan, this way it will keep printing both the names are getting a chance to print.

Code:

package main import ( "fmt" "time" ) func show(name string) { for i := 0; i< 6; i++ { time.Sleep(1 * time.Second) fmt.Println(name) } } func main() { go show("Ajay") show("Ranjan") }

Output:

Example #3

Code:

package main import ( "fmt" "time" ) func main() { fmt.Println("Hello , friends how are you") go func() { fmt.Println("How are you doing ") }() time.Sleep(2 * time.Second) fmt.Println("We have done with the discussion") }

Output:

Conclusion

From this tutorial we learned the basic concept of concurrency in the Go language, we learned the syntax and the working way of the concurrency in the Go language. We also focus on some of the examples with their explanation which revealed the working of the concurrency in go language.

Recommended Article

This is a guide to Golang Concurrency. Here we discuss the introduction, syntax and working of Golang Concurrency along with examples and code implementation. You can also go through our other suggested articles to learn more –

Update the detailed information about How Does Wait Function Work In Lua on the Cattuongwedding.com website. We hope the article's content will meet your needs, and we will regularly update the information to provide you with the fastest and most accurate information. Have a great day!