Multi-thread development-NSThrea and multi-thread nsthrea

Source: Internet
Author: User

Multi-thread development-NSThrea and multi-thread nsthrea

Create and start

Create a thread first, and then start

// Create    NSThread *thread = [[NSThread alloc] initWithTarget:self selector:@selector(run:) object:nil];    // Start    [thread start];  Create and start[NSThread detachNewThreadSelector: @ selector (run :) toTarget: self withObject: nil];

Implicitly create and start

[Self initialize mselectorinbackground: @ selector (run :) withObject: @ "mj"];

Other methods

Execute operations on the specified Thread

[Self defined mselector: @ selector (run) onThread: thread withObject: nil waitUntilDone: YES];

The code above indicates that the self run method is called on the thread.

The final YES indicates that the above Code will be blocked, and the above Code will pass only after the run method is executed in the thread.

Perform operations on the main thread

[Self defined mselecw.mainthread: @ selector (run) withObject: nil waitUntilDone: YES];

Execute operations in the current thread

[Self defined mselector: @ selector (run) withObject: nil];

// Cancel the thread - (void)cancel; // Start the thread - (void)start; // Attributes used to determine the status of a thread @property (readonly, getter=isExecuting) BOOL executing; @property (readonly, getter=isFinished) BOOL finished; @property (readonly, getter=isCancelled) BOOL cancelled; // Set and obtain the thread name -(void)setName:(NSString *)n; -(NSString *)name; // Obtain information about the current thread + (NSThread *)currentThread; // Obtain master thread Information + (NSThread *)mainThread; // Pause the current thread for a period of time or wait for a period of time + (void)sleepForTimeInterval:(NSTimeInterval)time; + (void)sleepUntilDate:(NSDate *)date;  Advantages and disadvantages:Advantage: NSThread is lighter than other multi-threaded solutions, allowing you to control thread objects more intuitively. Disadvantages: You need to manage the thread lifecycle and synchronize the thread. Thread Synchronization locks data with a certain amount of system overhead

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.