월루를 꿈꾸는 대학생

[Flutter] Isolate 개념 본문

Programing/플러터

[Flutter] Isolate 개념

하즈시 2023. 1. 27. 00:02
728x90

dart는 싱글 스레드 환경임 

즉 멀티 스레드 환경이 아니라서 과도한 처리를 여러개 하는 경우 플러터의 프레임이 끊기는 현상이 발견하기도 함 

 

그래서 isolate라는게 필요함 

별도의 메모리를 가지고 별도의 처리를 하는 친구 

 

완전한 별도의 장소이기 때문에 객체의 주소자체가 달라짐 

 

 

class MySingleton{

  static final MySingleton _instance = MySingleton._internal();

  factory MySingleton(){
    return _instance;
  }

}

 

해당 인스턴스를 만들고 보면 

같은 객체임

 

MySingleton my1 = MySingleton();
MySingleton my2 = MySingleton();

print(my1.hashCode);
print(my2.hashCode);
print(my1 == my2);

I/flutter ( 5509): 797000897
I/flutter ( 5509): 797000897
I/flutter ( 5509): true

 

다만 이걸 다른 isolate로 만드는 경우 싱글톤이라도 다른 객체가 되어버림 

 

Future<Isolate> getIsolate() async {

  Isolate? isolate;
  isolate = await Isolate.spawn<String>((message) {

    MySingleton my3 = MySingleton();
    print(my3.hashCode);
  }, '1234');


  return isolate;
}

I/flutter ( 5509): 681993582

 

728x90