Concurrency_(Thread)_Dart

 import "dart:isolate";


///Concurrency - (Thread) in Dart Programming
//Concurrency is the execution of several instruction sequences at the same time. It involves performing more
// than one task simultaneously. Dart uses Isolates as a tool for doing works in parallel.
// The dart:isolate package is Dart’s solution to taking single-threaded Dart code and
// allowing the application to make greater use of the hard-ware available.
// Isolates, as the name suggests, are isolated units of running code.
// The only way to send data between them is by passing messages, like the way you pass messages
// between the client and the server. An isolate helps the program to take advantage of multicore
// microprocessors out of the box.

///Multithreading in Flutter using Dart isolates
/// ● Method 1: Using compute
/// ● Method 2: Using Isolate.spawn

///What are isolates?
//An isolate is an abstraction on top of threads. It is similar to an
// event loop, with a few differences:
// ● An isolate has its own memory space
// ● It cannot share mutable values with other isolates
// ● Any data transmitted between isolates is duplicated

//An isolate is meant to run independently of other isolates. This offers a lot of benefits
// to the Dart VM, one of which is that garbage collection is easier.
/// One thing to keep in mind about creating parent isolates that, in turn, create child isolates
/// is that the child isolates will terminate if the parent does. Regardless of the hierarchy,
/// the parent isolate cannot access the memory of the child isolate.
// There are also a few components that are usually associated with isolates:

//● A ReceivePort: This is used by the isolate to receive data.
// Another instance of this can also be used by the parent isolate
// to send data to the spawned isolate
// ● A control port: This is a special port that allows its owner to
// have capabilities such as pausing or terminating the isolate
// ● Capability: These are object instances used for isolate
// authentication, i.e., whenever we wish to send control port
// commands like pause or terminate, we also need the
// corresponding instances of Capability that were used when
// the isolate was created, without which the command would
// fail

///compute :
///As mentioned above, there are a couple of ways to create isolates in
/// Flutter. One of the easiest is to use the compute function. This will
/// execute our code in a dierent isolate and return the results to our
/// main isolate.

/// EXAMPLE 1
void myMessage(var message)
{
print("Execution from myMessage... the message is $message");
}
// void main()
// {
// Isolate.spawn(myMessage, "Hello!!!");
// Isolate.spawn(myMessage, "Greetings!!!");
// Isolate.spawn(myMessage, "Welcome!!!");
//
// print("Execution from main1");
// print("Execution from main2");
// print("Execution from main3");
//
// }

///EXAMPLE 2
void main()
{
Isolate.spawn(myDesiredMessage, "This is my First Message!!!");
Isolate.spawn(myDesiredMessage, "This is my Second Message!!!");
Isolate.spawn(myDesiredMessage, "This is my Third Message!!!");
Isolate.spawn(myDesiredMessage, "This is my Fourth Message!!!");
Isolate.spawn(myDesiredMessage, "This is my Fifth Message!!!");
}
void myDesiredMessage(var message)
{
print("Hello This is message from myDesiredMessage method : $message");
}

Comments

Popular posts from this blog

Second GET API Calling with Bloc simple Example in Flutter

Stack Container Scrollable Card widget UI with Custom Widget

Pagination with Bloc Pattern in Flutter