Class: NSThread
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)
-
+ callStackReturnAddresses
Returns an array containing the call stack return addresses.
-
+ callStackSymbols
Returns an array containing the call stack symbols.
-
+ currentThread
Returns the thread object representing the current thread of execution.
-
+ detachNewThreadSelector:toTarget:withObject:
Detaches a new thread and uses the specified selector as the thread entry point.
-
+ exit
Terminates the current thread.
-
+ isMainThread
Returns a Boolean value that indicates whether the current thread is the main thread.
-
+ isMultiThreaded
Returns whether the application is multithreaded.
-
+ mainThread
Returns the NSThread object representing the main thread.
-
+ setThreadPriority:
Sets the current thread’s priority.
-
+ sleepForTimeInterval:
Sleeps the thread for a given time interval.
-
+ sleepUntilDate:
Blocks the current thread until the time specified.
-
+ threadPriority
Returns the current thread’s priority.
Instance Method Summary (collapse)
-
- cancel
Changes the cancelled state of the receiver to indicate that it should exit.
-
- init
Returns an initialized NSThread object.
-
- initWithTarget:selector:object:
Returns an NSThread object initialized with the given arguments.
-
- isCancelled
Returns a Boolean value that indicates whether the receiver is cancelled.
-
- isExecuting
Returns a Boolean value that indicates whether the receiver is executing.
-
- isFinished
Returns a Boolean value that indicates whether the receiver has finished execution.
-
- isMainThread
Returns a Boolean value that indicates whether the receiver is the main thread.
-
- main
The main entry point routine for the thread.
-
- name
Returns the name of the receiver.
-
- setName:
Sets the name of the receiver.
-
- setStackSize:
Sets the stack size of the receiver.
-
- setThreadPriority:
Sets the receiver’s priority.
-
- stackSize
Returns the stack size of the receiver.
-
- start
Starts the receiver.
-
- threadDictionary
Returns the thread object’s dictionary.
-
- threadPriority
Returns the reciever’s priority The priorities in this range are mapped to the operating system’s priority values.
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.
+ (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
+ (NSThread) currentThread
Returns the 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.
+ (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.
+ (Boolean) isMainThread
Returns a Boolean value that indicates whether the current thread is the main thread.
+ (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.
+ (Boolean) setThreadPriority(priority)
Sets the current thread’s priority. The priorities in this range are mapped to the operating system’s priority values.
+ (Object) sleepForTimeInterval(ti)
Sleeps the thread for a given time interval. No run loop processing occurs while the thread is blocked.
+ (Object) sleepUntilDate(aDate)
Blocks the current thread until the time specified. No run loop processing occurs while the thread is blocked.
+ (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.
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.
- (Object) init
- (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.
- (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.
- (Boolean) isExecuting
Returns a Boolean value that indicates whether the receiver is executing.
- (Boolean) isFinished
Returns a Boolean value that indicates whether the receiver has finished execution.
- (Boolean) isMainThread
Returns a Boolean value that indicates whether the receiver is the main thread.
- (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.
- (String) name
Returns the name of the receiver.
- (Object) setName(n)
Sets the name of the receiver.
- (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.
- (Object) setThreadPriority(priority)
Sets the receiver’s priority. The priorities in this range are mapped to the operating system’s priority values.
- (Integer) stackSize
Returns the stack size of the receiver.
- (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.
- (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.
- (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.