Thread<T> class final

Emulates Java's Thread class using Dart's Isolate.

A Thread represents a separate unit of execution and encapsulates a Dart Isolate under the hood. Each Thread has its own memory space and runs independently from others.

This class provides Java-style threading behaviors such as:

  • start()
  • join()
  • interrupt()
  • static utilities like sleep(), currentThread(), and getThreadLocal(id)

Example usage:

void entry(ThreadMessage message) {
  print("Thread started with data: ${message.data}");
  message.replyPort.send('done');
}

void main() async {
  final thread = Thread(entry, initialMessage: 'Hello', debugName: 'worker-1');
  await thread.start();
  await thread.join();
  print('Thread completed.');
}
Annotations

Constructors

Thread(dynamic _entryPoint(ThreadMessage), {dynamic initialMessage, String? debugName})
Emulates Java's Thread class using Dart's Isolate.

Properties

hashCode → int
The hash code for this object.
no setterinherited
initialMessage → dynamic
final
isAlive → bool
Returns true if the underlying Isolate is still running.
no setter
runtimeType → Type
A representation of the runtime type of the object.
no setterinherited

Methods

interrupt({int priority = Isolate.immediate}) → void
Forcefully terminates the underlying Isolate.
join() → Future<void>
Waits for the Isolate to complete execution.
noSuchMethod(Invocation invocation) → dynamic
Invoked when a nonexistent method or property is accessed.
inherited
start() → Future<void>
Starts execution of this Thread in a new Isolate.
toString() → String
A string representation of this object.
inherited

Operators

operator ==(Object other) → bool
The equality operator.
inherited

Static Properties

currentThreadIsolate → Isolate
Returns the Isolate object representing the currently executing isolate.
no setter
currentThreadName → String?
Returns the name of the currently executing isolate.
no setter

Static Methods

currentThread() → Thread?
Returns the Thread instance representing the currently executing Isolate.
getThreadLocal<T>(int id) → LocalThread<T>
Retrieves a LocalThread instance by its unique ID.
sleep(Duration duration) → Future<void>
Asynchronous sleep operation for the current isolate.