This is the 20th day of my participation in the November Gwen Challenge. Check out the details: The last Gwen Challenge 2021.

We looked at asynchronous programming in DART earlier, but DART is a single-threaded development, and asynchronous programming here is not the same as asynchronous multithreading in iOS. Dart has similar multithreading, so let’s take a look at that.

1. Isolate

Let’s start with an example

void IsolateTest(){

  print('A');
  Future(()=>print('B'));
  sleep(Duration(seconds: 1));
  print('C');
}
Copy the code

The way we think about it in iOS is that the main thread is blocked, and the cross-step thread will execute and print the ABC, but in the Future it’s single thread, so one thread asynchronously just joins the task queue, and the main thread is blocked, and the task in the queue won’t execute, so finally it’s ACB.

void IsolateTest(){

  print('A');
  Isolate.spawn(func,10);
  sleep(Duration(seconds: 1));
  print('C');
}

func(int num) = >print('B');
Copy the code

We use Isolate to do asynchronous operations, pass in a value, and print ABC, which is what we want

Let’s verify our understanding of multithreading

void IsolateTest(){

  print('A');
  Isolate.spawn(func,10);
  Isolate.spawn(func2,10);
  Isolate.spawn(func,10);
  Isolate.spawn(func2,10);
  Isolate.spawn(func,10);
  Isolate.spawn(func2,10);
  Isolate.spawn(func,10);
  Isolate.spawn(func2,10);
  sleep(Duration(seconds: 3));
  print('C');
}

func(int num) = >print('B1');
func2(int num) = >print('B2');
Copy the code

According to our previous understanding, it should be A followed by B1B2B1B2, and the actual print is random, not synchronous.

This is similar to what we have in iOSThe child threadBut it’s not exactly the same, we define a variable externally, and thenIsolate the threadTo change its value before the main thread prints.

void IsolateTest(){

  print('A');
  Isolate.spawn(func,10);
  sleep(Duration(seconds: 3));
  print('a= $a');
  print('C');
}
int a = 1;
func(int num) {
  a =num;
  print('a= $a');
}
Copy the code

IsolateMore like aprocessThat means it hasSeparate memory spaceAnd notRob resourcesSo we don’t needlockTo deal with. But what do we do if we want to change the value


void IsolateTest(){

  print('A');

  // Create a prot
  ReceivePort port = ReceivePort();

  Isolate.spawn(func,port.sendPort);/ / pass


  port.listen((message) { / / to monitor
    a = message;
    print(message);
  });
  
  // sleep(Duration(seconds: 1));
  print('a= $a');
  print('C');
}
int a = 1;
// the method uses SendPort to accept
func(SendPort sendPort) {
  sendPort.send(101);// Send a message
  a = 101;
  print('a= $a');
}
Copy the code

We use ReceivePort to pass the value and port.listen to listen for the value change. Our main thread is making the corresponding change and we need to close it manually after using it.

Future<void> IsolateTest() async {

  print('A');

  // Create a prot
  ReceivePort port = ReceivePort();

 Isolate iso = await Isolate.spawn(func,port.sendPort);/ / pass


  port.listen((message) { / / to monitor
    a = message;
    print(message);
    port.close();
    iso.kill();
  });

  sleep(Duration(seconds: 3));
  print('a= $a');
  print('C');
}
Copy the code

We received the close port with await and then killed the thread iso.kill

2. compute

Compute is the encapsulation of Isolate, the usage method is similar.

computeTest(){

  print('A');
  compute(func2,10);
  sleep(Duration(seconds: 2));
  print('C');
}

func2(int num) {print('Here comes the child thread');
}
Copy the code

Print result:

We want to change the internal values we useawaitreceive

int a = 1;
computeTest() async{

  print('A');
  a = await compute(func2,10);
  sleep(Duration(seconds: 2));
  print('C');
  print('a = $a');
}

int func2(int num) {print('Here comes the child thread');
  return 100;
}
Copy the code

The printed result is:

We change variables inside the child thread, we don’t change variables outside the print, it’s essentially the same as our Isolate.

And compute doesn’t need to be turned off manually.