static ThreadLocal<Uint8List> threadBuffer = new ThreadLocal<Uint8List>() {
@Override
public byte[] initialValue() {
return new byte[512];
}
};
In Java, ThreadLocal is a class that provides thread-local variables. These are variables that are specific to a particular thread and are not shared among other threads.
So what is equivalent of this code snippets in dart.
The dart equivalent is simply:
In dart you have isolates instead of threads. Isolates do not share memory (state of variables), so the behavior is similar to
ThreadLocal
.Consider this example:
Which prints out:
Notice that
threadBuffer
on the main isolate was unaffected by changes tothreadBuffer
in each of the spawned isolates. This is because the spawned isolates are working on their own independent copy ofthreadBuffer
rather than sharing a single instance ofthreadBuffer
.