Multithreading--Nsthread

Source: Internet
Author: User

Nsthread
    • Nsthread

      • A Nsthread object represents a thread
    • Several ways to create threads

    • Alloc/init
    //1. Create a threadNjthread *thread = [[Njthread alloc] initwithtarget:self selector: @selector (Demo:)Object:@"LNJ"]; //set the thread name[Thread SetName:@"XMG"]; //set the priority of a thread//priority only indicates a greater likelihood of being called by the CPU[Thread SetThreadPriority:1.0]; //2. Start the thread[Thread start];
    • Related usage of threads
// get the main thread // Is the main thread // get current thread Nsthread *current = [Nsthread CurrentThread]; // the name of the thread -(void) SetName: (NSString *) n; -(NSString *) name;
    • Other ways to create threads
      • Advantages: Simple and fast
      • Cons: Unable to set the thread in more detail
// 1.创建线程
[Nsthread Detachnewthreadselector: @selector (Demo:) totarget:self withobject:@ "lnj"];
// 1.创建线程// 注意: Swift中不能使用, 苹果认为这个方法不安全
[Self Performselectorinbackground: @selector (Demo:) Withobject:@ "lnj"];
    • Thread state

Start Thread -(void) start; // go to Ready status, run status. When the thread task finishes executing, automatically enters the death state to block (pause) the thread + (void) Sleepuntildate: (NSDate *) date; + (void) Sleepfortimeinterval: (Nstimeinterval) ti; // Enter blocking State Force Stop thread + (void) exit; // into the state of death
注意:一旦线程停止(死亡)了,就不能再次开启任务
    • The security hidden trouble of multithreading

      • Data confusion and data security issues can easily arise when multiple threads access the same resource
    • Thread Lock

      • Locked code can only be executed by one thread at a time
// code that needs to be locked  }
    • Advantages and disadvantages of mutual exclusion lock: can effectively prevent the data security problem caused by multi-thread snatch resources disadvantage: the need to consume a lot of CPU resources

    • Mutual exclusion Lock attention point

      • Lock 1 copies of code with only 1 locks, with multiple locks is not valid
      • The larger the locking range, the worse the performance
    • Atomic and non-atomic properties

      • Atomic: Thread-safe, requires a lot of resources
      • Nonatomic: Non-thread-safe, suitable for small memory mobile devices
    • Spin Lock & Mutual Exclusion lock

      • Common denominator ensures that only one thread executes a locked range of code at the same time
      • Different points
        • Mutex: If another thread is found to be executing the locked code, the thread goes into a "hibernate" state, waits for the other thread to finish, and after the lock is opened, the thread is "awakened"
        • Spin lock: If another thread is found to be executing the locked code, the thread will "wait" for the lock code to finish executing! Spin lock is more suitable for executing very short code!
    • Inter-thread communication
      • Child threads do time-consuming operations, the main thread updates the data
[Self performselectorinbackground: @selector (download) withobject:nil];     /*      Whether the Waituntildone waits for the invoked method to complete, it is possible to wait for the calling method to complete!     YES: Wait for the called thread to finish executing and then execute the following code     No: You can execute the following code     without waiting for the called thread to execute */[Self performselectoronmainthread: @selector (showImage:) Withobject:[uiimage Imagewithdata:data] Waituntildone:yes];

Multithreading--Nsthread

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.