An Thread object controls a thread of execution. Use this class when you want to have an Objective-C method run in its own thread of execution. Threads are especially useful when you need to perform a lengthy task, but don’t want it to block the execution of the rest of the application. In particular, you can use threads to avoid blocking the main thread of the application, which handles user interface and event-related actions. Threads can also be used to divide a large job into several smaller jobs, which can lead to performance increases on multi-core computers.


The Thread class supports semantics similar to those of Operation for monitoring the runtime condition of a thread. You can use these semantics to cancel the execution of a thread or determine if the thread is still executing or has finished its task. Canceling a thread requires support from your thread code; see the description for cancel() for more information.

Subclassing Notes

You can subclass Thread and override the main() method to implement your thread’s main entry point. If you override main(), you do not need to invoke the inherited behavior by calling super.


Initializing an NSThread Object


Returns an initialized NSThread object.

init(target: Any, selector: Selector, object: Any?)

Returns an NSThread object initialized with the given arguments.

Starting a Thread

class func detachNewThreadSelector(Selector, toTarget: Any, with: Any?)

Detaches a new thread and uses the specified selector as the thread entry point.

func start()

Starts the receiver.

func main()

The main entry point routine for the thread.

Stopping a Thread

class func sleep(until: Date)

Blocks the current thread until the time specified.

class func sleep(forTimeInterval: TimeInterval)

Sleeps the thread for a given time interval.

class func exit()

Terminates the current thread.

func cancel()

Changes the cancelled state of the receiver to indicate that it should exit.

Determining the Thread’s Execution State

var isExecuting: Bool

A Boolean value that indicates whether the receiver is executing.

var isFinished: Bool

A Boolean value that indicates whether the receiver has finished execution.

var isCancelled: Bool

A Boolean value that indicates whether the receiver is cancelled.

Working with the Main Thread

class var isMainThread: Bool
var isMainThread: Bool

A Boolean value that indicates whether the receiver is the main thread.

class var main: Thread

Querying the Environment

class func isMultiThreaded()

Returns whether the application is multithreaded.

class var current: Thread

Returns the thread object representing the current thread of execution.

class var callStackReturnAddresses: [NSNumber]

Returns an array containing the call stack return addresses.

class var callStackSymbols: [String]

Working with Thread Properties

var threadDictionary: NSMutableDictionary

The thread object's dictionary.

var name: String?

The name of the receiver.

var stackSize: Int

The stack size of the receiver, in bytes.

Working with Thread Priorities

class func threadPriority()

Returns the current thread’s priority.

var threadPriority: Double

The receiver’s priority

class func setThreadPriority(Double)

Sets the current thread’s priority.


static let NSThreadWillExit: NSNotification.Name

An NSThread object posts this notification when it receives the exit() message, before the thread exits. Observer methods invoked to receive this notification execute in the exiting thread, before it exits.

static let NSWillBecomeMultiThreaded: NSNotification.Name

Posted when the first thread is detached from the current thread. The NSThread class posts this notification at most once—the first time a thread is detached using detachNewThreadSelector(_:toTarget:with:) or the start() method. Subsequent invocations of those methods do not post this notification. Observers of this notification have their notification method invoked in the main thread, not the new thread. The observer notification methods always execute before the new thread begins executing.


Inherits From

Conforms To