Class: NSThread

Inherits:
NSObject show all

Overview

An NSThread 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.

Class Method Summary (collapse)

Instance Method Summary (collapse)

Methods inherited from NSObject

#!, #!=, #!~, #, #==, #===, #=~, #Rational, #__callee__, #__method__, #__send__, #__type__, `, alloc, allocWithZone:, #autoContentAccessingProxy, autoload, autoload?, autorelease_pool, #awakeAfterUsingCoder:, binding, block_given?, caller, cancelPreviousPerformRequestsWithTarget:, cancelPreviousPerformRequestsWithTarget:selector:object:, catch, class, classFallbacksForKeyedArchiver, #classForCoder, #classForKeyedArchiver, classForKeyedUnarchiver, #clone, conformsToProtocol:, #copy, copyWithZone:, #dealloc, #define_singleton_method, description, display, #doesNotRecognizeSelector:, #dup, #enum_for, #eql?, #equal?, #extend, fail, #finalize, format, #forwardInvocation:, #forwardingTargetForSelector:, framework, #freeze, #frozen?, getpass, gets, global_variables, initialize, #initialize_clone, #initialize_copy, #initialize_dup, #inspect, instanceMethodForSelector:, instanceMethodSignatureForSelector:, #instance_eval, #instance_exec, #instance_of?, #instance_variable_defined?, #instance_variable_get, #instance_variable_set, #instance_variables, instancesRespondToSelector:, isSubclassOfClass:, #is_a?, iterator?, #kind_of?, lambda, load, load_bridge_support_file, load_plist, local_variables, loop, #method, #methodForSelector:, #methodSignatureForSelector:, #methods, #mutableCopy, mutableCopyWithZone:, new, #nil?, open, p, #performSelector:onThread:withObject:waitUntilDone:, #performSelector:onThread:withObject:waitUntilDone:modes:, #performSelector:withObject:afterDelay:, #performSelector:withObject:afterDelay:inModes:, #performSelectorInBackground:withObject:, #performSelectorOnMainThread:withObject:waitUntilDone:, #performSelectorOnMainThread:withObject:waitUntilDone:modes:, print, printf, #private_methods, proc, #protected_methods, #public_method, #public_methods, #public_send, putc, puts, raise, rand, readline, readlines, #replacementObjectForCoder:, #replacementObjectForKeyedArchiver:, require, resolveClassMethod:, resolveInstanceMethod:, #respond_to?, #respond_to_missing?, select, #send, setVersion:, #singleton_methods, sprintf, srand, superclass, #taint, #tainted?, #tap, test, throw, #to_plist, #to_s, trace_var, trap, #trust, #untaint, untrace_var, #untrust, #untrusted?, version

Constructor Details

This class inherits a constructor from NSObject

Dynamic Method Handling

This class handles dynamic methods through the method_missing method in the class NSObject

Class Method Details

+ (Array) callStackReturnAddresses

Returns an array containing the call stack return addresses.

Returns:

  • (Array)

    An array containing the call stack return addresses. This value is nil by default.

+ (Array) callStackSymbols

Returns an array containing the call stack symbols. This method returns an array of strings describing the call stack backtrace of the current thread at the moment this method was called. The format of each string is non-negotiable and is determined by the backtrace_symbols() API

Returns:

  • (Array)

    An array containing the call stack symbols.

+ (NSThread) currentThread

Returns the thread object representing the current thread of execution.

Returns:

  • (NSThread)

    A thread object representing the current thread of execution.

+ (Object) detachNewThreadSelector(aSelector, toTarget:aTarget, withObject:anArgument)

Detaches a new thread and uses the specified selector as the thread entry point. For non garbage-collected applications, the method aSelector is responsible for setting up an autorelease pool for the newly detached thread and freeing that pool before it exits. Garbage-collected applications do not need to create an autorelease pool.The objects aTarget and anArgument are retained during the execution of the detached thread, then released. The detached thread is exited (using the exit class method) as soon as aTarget has completed executing the aSelector method.If this thread is the first thread detached in the application, this method posts the NSWillBecomeMultiThreadedNotification with object nil to the default notification center.

Parameters:

  • aSelector (Symbol)

    The selector for the message to send to the target. This selector must take only one argument and must not have a return value.

  • aTarget (Object)

    The object that will receive the message aSelector on the new thread.

  • anArgument (Object)

    The single argument passed to the target. May be nil.

Returns:

+ (Object) exit

Terminates the current thread. This method uses the currentThread class method to access the current thread. Before exiting the thread, this method posts the NSThreadWillExitNotification with the thread being exited to the default notification center. Because notifications are delivered synchronously, all observers of NSThreadWillExitNotification are guaranteed to receive the notification before the thread exits.Invoking this method should be avoided as it does not give your thread a chance to clean up any resources it allocated during its execution.

Returns:

+ (Boolean) isMainThread

Returns a Boolean value that indicates whether the current thread is the main thread.

Returns:

  • (Boolean)

    YES if the current thread is the main thread, otherwise NO.

+ (Boolean) isMultiThreaded

Returns whether the application is multithreaded. An application is considered multithreaded if a thread was ever detached from the main thread using either detachNewThreadSelector:toTarget:withObject: or start. If you detached a thread in your application using a non-Cocoa API, such as the POSIX or Multiprocessing Services APIs, this method could still return NO. The detached thread does not have to be currently running for the application to be considered multithreaded—this method only indicates whether a single thread has been spawned.

Returns:

  • (Boolean)

    YES if the application is multithreaded, NO otherwise.

+ (NSThread) mainThread

Returns the NSThread object representing the main thread.

Returns:

+ (Boolean) setThreadPriority(priority)

Sets the current thread’s priority. The priorities in this range are mapped to the operating system’s priority values.

Parameters:

  • priority (Float)

    The new priority, specified with a floating point number from 0.0 to 1.0, where 1.0 is highest priority.

Returns:

  • (Boolean)

    YES if the priority assignment succeeded, NO otherwise.

+ (Object) sleepForTimeInterval(ti)

Sleeps the thread for a given time interval. No run loop processing occurs while the thread is blocked.

Parameters:

  • ti (NSTimeInterval)

    The duration of the sleep.

Returns:

+ (Object) sleepUntilDate(aDate)

Blocks the current thread until the time specified. No run loop processing occurs while the thread is blocked.

Parameters:

  • aDate (NSDate)

    The time at which to resume processing.

Returns:

+ (Float) threadPriority

Returns the current thread’s priority. The priorities in this range are mapped to the operating system’s priority values. A “typical” thread priority might be 0.5, but because the priority is determined by the kernel, there is no guarantee what this value actually will be.

Returns:

  • (Float)

    The current thread’s priority, which is specified by a floating point number from 0.0 to 1.0, where 1.0 is highest priority.

Instance Method Details

- (Object) cancel

Changes the cancelled state of the receiver to indicate that it should exit. The semantics of this method are the same as those used for the NSOperation object. This method sets state information in the receiver that is then reflected by the isCancelled method. Threads that support cancellation should periodically call the isCancelled method to determine if the thread has in fact been cancelled, and exit if it has been. For more information about cancellation and operation objects, see NSOperation Class Reference.

Returns:

- (Object) init

Returns an initialized NSThread object. This is the designated initializer for NSThread.

Returns:

- (Object) initWithTarget(target, selector:selector, object:argument)

Returns an NSThread object initialized with the given arguments. For non garbage-collected applications, the method selector is responsible for setting up an autorelease pool for the newly detached thread and freeing that pool before it exits. Garbage-collected applications do not need to create an autorelease pool.The objects target and argument are retained during the execution of the detached thread. They are released when the thread finally exits.

Parameters:

  • target (Object)

    The object to which the message specified by selector is sent.

  • selector (Symbol)

    The selector for the message to send to target. This selector must take only one argument and must not have a return value.

  • argument (Object)

    The single argument passed to the target. May be nil.

Returns:

  • (Object)

    An NSThread object initialized with the given arguments.

- (Boolean) isCancelled

Returns a Boolean value that indicates whether the receiver is cancelled. If your thread supports cancellation, it should call this method periodically and exit if it ever returns YES.

Returns:

  • (Boolean)

    YES if the receiver has been cancelled, otherwise NO.

- (Boolean) isExecuting

Returns a Boolean value that indicates whether the receiver is executing.

Returns:

  • (Boolean)

    YES if the receiver is executing, otherwise NO.

- (Boolean) isFinished

Returns a Boolean value that indicates whether the receiver has finished execution.

Returns:

  • (Boolean)

    YES if the receiver has finished execution, otherwise NO.

- (Boolean) isMainThread

Returns a Boolean value that indicates whether the receiver is the main thread.

Returns:

  • (Boolean)

    YES if the receiver is the main thread, otherwise NO.

- (Object) main

The main entry point routine for the thread. The default implementation of this method takes the target and selector used to initialize the receiver and invokes the selector on the specified target. If you subclass NSThread, you can override this method and use it to implement the main body of your thread instead. If you do so, you do not need to invoke super. You should never invoke this method directly. You should always start your thread by invoking the start method.

Returns:

- (String) name

Returns the name of the receiver.

Returns:

  • (String)

    The name of the receiver.

- (Object) setName(n)

Sets the name of the receiver.

Parameters:

  • n (String)

    The name for the receiver.

Returns:

- (Object) setStackSize(s)

Sets the stack size of the receiver. You must call this method before starting your thread. Setting the stack size after the thread has started changes the attribute size (which is reflected by the stackSize method), but it does not affect the actual number of pages set aside for the thread.

Parameters:

  • s (Integer)

    The stack size for the receiver. This value must be in bytes and a multiple of 4KB.

Returns:

- (Object) setThreadPriority(priority)

Sets the receiver’s priority. The priorities in this range are mapped to the operating system’s priority values.

Parameters:

  • priority (Float)

    The new priority, specified with a floating point number from 0.0 to 1.0, where 1.0 is highest priority.

Returns:

- (Integer) stackSize

Returns the stack size of the receiver.

Returns:

  • (Integer)

    The stack size of the receiver, in bytes.

- (Object) start

Starts the receiver. This method spawns the new thread and invokes the receiver’s main method on the new thread. If you initialized the receiver with a target and selector, the default main method invokes that selector automatically. If this thread is the first thread detached in the application, this method posts the NSWillBecomeMultiThreadedNotification with object nil to the default notification center.

Returns:

- (Hash) threadDictionary

Returns the thread object’s dictionary. You can use the returned dictionary to store thread-specific data. The thread dictionary is not used during any manipulations of the NSThread object—it is simply a place where you can store any interesting data. For example, Foundation uses it to store the thread’s default NSConnection and NSAssertionHandler instances. You may define your own keys for the dictionary.

Returns:

  • (Hash)

    The thread object’s dictionary.

- (Float) threadPriority

Returns the reciever’s priority The priorities in this range are mapped to the operating system’s priority values. A “typical” thread priority might be 0.5, but because the priority is determined by the kernel, there is no guarantee what this value actually will be.

Returns:

  • (Float)

    The thread’s priority, which is specified by a floating point number from 0.0 to 1.0, where 1.0 is highest priority.