Dart was traditionally designed to create single-page applications. And we also know that most computers, even mobile platforms, have multi-core CPUs. To take advantage of all those cores, developers traditionally use shared-memory threads running concurrently. However, shared-state concurrency is error-prone and can lead to complicated code. Instead of threads, all Dart code runs inside of isolates. Each isolate has its own memory heap, ensuring that no isolate’s state is accessible from any other isolate.
The isolates and threads are different than each other as in threads memory are shared whereas in isolates it is not. Moreover isolates talk to each other via passing messages.
To use isolates you have to add import 'dart:isolate'; statement in your program code.
Creating An Isolate In Dart
To create an isolate we make use of .spawn() method in Dart.
Syntax: Isolate isolate_name = await Isolate.spawn( parameter );
This parameter represents the port that will receive the message back.
Destroying An Isolate In Dart
To destroy the isolate we make use of .kill() method in Dart.
Syntax: isolate_name.kill( parameters );
We generally use spawn() and kill() together in a single program.
Example: Creating an isolate in Dart.
--------------Starting Geek Isolate-------------- Press enter key to quit Welcome to GeeksForGeeks 1 Welcome to GeeksForGeeks 2 Welcome to GeeksForGeeks 3 Welcome to GeeksForGeeks 4 Welcome to GeeksForGeeks 5 Welcome to GeeksForGeeks 6 Welcome to GeeksForGeeks 7 --------------Stopping Geek Isolate-------------- GoodBye Geek!
Hitting enter after seventh output.
- Concept of Callable Classes in Dart
- Dart - Concept of Inheritance
- How to Combine Lists in Dart?
- Basics of Numbers in Dart
- Dart - Super and This keyword
- Dart - Standard Input Output
- Generators in Dart
- Dart - Classes And Objects
- Operators in Dart
- Dart - Loops
- Switch Case in Dart
- Dart - Loop Control Statements (Break and Continue)
- Dart Programming - Functions
- Instance and class methods in Dart
- Abstract Classes in Dart
- Labels in Dart
- Dart Programming - List
- Different Types of Functions in Dart
- Dart - Splitting of String
- Method Overriding in Dart
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to email@example.com. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.