IOS multithreaded Nsthread Understanding and scenario examples
Source: Internet
Author: User
<span id="Label3"></p><p><p>Nsthread is a relatively lightweight, multi-threading approach compared to GCD and Nsoperationquene.</p></p><p><p>But at the same time, its disadvantage is the need to manage the Thread's life cycle, as well as thread synchronization, while the other two do not need to manage themselves.</p></p><p><p>Common methods are described:</p></p><p><p></p></p>one, Gets the current thread<pre><pre>Nsthread *current = [nsthread currentthread];</pre></pre>second, Get the main thread<pre><pre>Nsthread *main = [nsthread mainthread];</pre></pre><p><p></p></p><p><p><strong><span style="font-size: 1.5em; line-height: 1.5">third, the creation of Nsthread</span></strong></p></p><pre><pre>1//initialize Thread 2 nsthread *thread = [[[nsthread alloc] initwithtarget:self selector: @selector (run:) object:@ "mj"] autorelease ; 3//open Thread 4 [thread start];</pre></pre><p><p></p></p>. Static methods<pre><pre>+ (void) detachnewthreadselector: (SEL) selector totarget: (id) target withobject: (id) argument;</pre></pre><pre><pre>[nsthread detachnewthreadselector: @selector (run:) totarget:self withobject:@ ""];</pre></pre><p><p>After executing the above code, <strong>a new thread is started</strong> immediately, and the Run method of self is called on this thread.</p></p><p><p></p></p>. Implicitly creating Threads<pre><pre></pre></pre><p><p>A new thread is created implicitly, and the Run method of self is called on this thread.</p></p><p><p></p></p>Iv. pausing the current thread<pre><pre>[nsthread sleepfortimeinterval:2];</pre></pre><pre><pre>NSDate *date = [nsdate datewithtimeinterval:2 sincedate:[nsdate date]]; [nsthread sleepuntildate:date];</pre></pre><p><p>Both of the above are pausing the current thread for 2 seconds</p></p><p><p></p></p><p><p>Exit thread</p></p><pre><pre>[nsthread exit];</pre></pre><p><p></p></p><p><p></p></p>V. Other operations on a thread 1. Perform actions on the specified thread<pre><pre></pre></pre><p><p>* The above code means that the run method of self is called on thread</p></p><p><p>* The last Yes means: the code above will block, and when the Run method finishes executing the thread, the code above will pass</p></p><p><p></p></p>2. Perform actions on the main thread<pre><pre>[self performselectoronmainthread: @selector (run) withobject:nil waituntildone:yes]; </pre></pre><p><p>Call the Run method of self on the main thread</p></p><p><p></p></p>3. Performing actions on the current thread<pre><pre></pre></pre><p><p>Call the Run method of self on the current thread</p></p><p><p>Scenario 1:</p></p><p><p>Asynchronously downloads a picture that finishes downloading in the UI Display.</p></p><p><p>Code implementation:</p></p><pre><span style="color: #000000"><span style="color: #000000">//open sub-thread Download<br>[nsthread detachnewthreadselector: @selector (downloadpic) totarget:self withobject:nil];</span></span><span style="color: #008000"><span style="color: #008000">//</span></span><span style="color: #008000"><span style="color: #008000">Download Image</span></span>-(<span style="color: #0000ff"><span style="color: #0000ff">void</span></span><span style="color: #000000"><span style="color: #000000">) Downloadpic{nsurl</span></span>*url = [nsurl urlwithstring:<span style="color: #800000"><span style="color: #800000">@"</span></span><span style="color: #800000"><span style="color: #800000">Https://res.wx.qq.com/mpres/htmledition/images/mp_qrcode218877.gif</span></span><span style="color: #800000"><span style="color: #800000">"</span></span><span style="color: #000000"><span style="color: #000000">]; NSData</span></span>*pic =<span style="color: #000000"><span style="color: #000000">[nsdata datawithcontentsofurl:url]; UIImage</span></span>*img =<span style="color: #000000"><span style="color: #000000">[UIImage imagewithdata:pic]; Back to the main thread display [self performselectoronmainthread: @selector (showimg:) withobject:img waituntildone:yes];}</span></span>-(<span style="color: #0000ff"><span style="color: #0000ff">void</span></span>) Showimg: (UIImage *<span style="color: #000000"><span style="color: #000000">) image{</span></span><span style="color: #008000"><span style="color: #008000">//</span></span><span style="color: #008000"><span style="color: #008000">imageview.image = Image</span></span>NSLog (<span style="color: #800000"><span style="color: #800000">@"</span></span><span style="color: #800000"><span style="color: #800000">Show pic</span></span><span style="color: #800000"><span style="color: #800000">"</span></span><span style="color: #000000"><span style="color: #000000">);}</span></span></pre><p><p> </p></p><p><p></p></p>six, multi-threaded Security and lock<p><p>Description: multiple threads accessing the same resource may have security issues such as data confusion, and the workaround is to lock up the necessary code Snippets.</p></p><p><p>Note: in OC The mutex is used @synchronized (self) {}, the Objc_sync_enter (self) and the Objc_sync_exit (self) method can be used in swift, note that both methods must be used in pairs, Put the code you want to lock in the Middle.</p></p><p><p>Scenario 1:</p></p><p><p>Take the ticket sales as an example: 3 ticket sales at the same time, the total number of tickets is 10.</p></p><pre>- (<span style="color: #0000ff"><span style="color: #0000ff">void</span></span><span style="color: #000000"><span style="color: #000000">) Sailticket {nsthread</span></span>*thread1 = [[nsthread alloc] initwithtarget:self selector: @selector (sale)<span style="color: #0000ff"><span style="color: #0000ff">Object</span></span><span style="color: #000000"><span style="color: #000000">: nil]; Nsthread</span></span>*thread2 = [[nsthread alloc] initwithtarget:self selector: @selector (sale)<span style="color: #0000ff"><span style="color: #0000ff">Object</span></span><span style="color: #000000"><span style="color: #000000">: nil]; Nsthread</span></span>*thread3 = [[nsthread alloc] initwithtarget:self selector: @selector (sale)<span style="color: #0000ff"><span style="color: #0000ff">Object</span></span><span style="color: #000000"><span style="color: #000000">: nil]; Thread1.name</span></span>=<span style="color: #800000"><span style="color: #800000">@"</span></span><span style="color: #800000"><span style="color: #800000">Conductor 1</span></span><span style="color: #800000"><span style="color: #800000">"</span></span><span style="color: #000000"><span style="color: #000000">; Thread2.name</span></span>=<span style="color: #800000"><span style="color: #800000">@"</span></span><span style="color: #800000"><span style="color: #800000">Conductor 2</span></span><span style="color: #800000"><span style="color: #800000">"</span></span><span style="color: #000000"><span style="color: #000000">; Thread3.name</span></span>=<span style="color: #800000"><span style="color: #800000">@"</span></span><span style="color: #800000"><span style="color: #800000">Conductor 3</span></span><span style="color: #800000"><span style="color: #800000">"</span></span><span style="color: #000000"><span style="color: #000000">; [thread1 start]; [thread2 start]; [thread3 start]; }</span></span>-(<span style="color: #0000ff"><span style="color: #0000ff">void</span></span><span style="color: #000000"><span style="color: #000000">) sale{</span></span><span style="color: #0000ff"><span style="color: #0000ff"></span> while</span>(<span style="color: #800080"><span style="color: #800080">1</span></span><span style="color: #000000"><span style="color: #000000">) {@synchronized (self) {</span></span><span style="color: #0000ff"><span style="color: #0000ff"></span> for</span>(<span style="color: #0000ff"><span style="color: #0000ff">int</span></span>i =<span style="color: #800080"><span style="color: #800080">0</span></span>; i<<span style="color: #800080"><span style="color: #800080">10000</span></span>; i++<span style="color: #000000"><span style="color: #000000">) { </span></span><span style="color: #008000"><span style="color: #008000">//</span></span><span style="color: #008000"><span style="color: #008000">Analog Delay</span></span><span style="color: #000000"><span style="color: #000000"> } </span></span><span style="color: #0000ff"><span style="color: #0000ff">if</span></span>(_ticketcount><span style="color: #800080"><span style="color: #800080">0</span></span><span style="color: #000000"><span style="color: #000000">) {_ticketcount</span></span>--<span style="color: #000000"><span style="color: #000000">; NSLog (</span></span><span style="color: #800000"><span style="color: #800000">@"</span></span><span style="color: #800000"><span style="color: #800000">%@ sold a ticket, and A%lu.</span></span><span style="color: #800000"><span style="color: #800000">"</span></span>, [nsthread currentthread].name, (unsigned<span style="color: #0000ff"><span style="color: #0000ff">Long</span></span><span style="color: #000000"><span style="color: #000000">) _ticketcount); }</span></span><span style="color: #0000ff"><span style="color: #0000ff">Else</span></span><span style="color: #000000"><span style="color: #000000">{NSLog (</span></span><span style="color: #800000"><span style="color: #800000">@"</span></span><span style="color: #800000"><span style="color: #800000">tickets are sold out</span></span><span style="color: #800000"><span style="color: #800000">"</span></span><span style="color: #000000"><span style="color: #000000">); </span></span><span style="color: #0000ff"><span style="color: #0000ff"></span> break</span><span style="color: #000000"><span style="color: #000000">; } } } }</span></span></pre><p><p></p></p><p><p></p></p><p><p>It can be clearly seen that each conductor is strictly in accordance with the 10 decline in the way of ticket sales, there is no more than the number of votes and less.</p></p><p><p>If no lock is added:</p></p><p><p></p></p><p><p></p></p><p><p></p></p><p><p></p></p><p><p> </p></p><p><p></p></p><p><p>IOS multithreaded Nsthread Understanding and scenario examples</p></p></span>
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