Interview question 08-multithreaded Network

Source: Internet
Author: User
Tags gcd

@font-face {font-family: "Arial";} @font-face {font-family: "Wingdings";} @font-face {font-family: "Song Body";} @font-face {font-family: "Cambria Math";} @font-face {font-family: "@ Song body";} @font-face {font-family: "Cambria";} @font-face {font-family: "Consolas";} P.msonormal, Li. MsoNormal, Div. MsoNormal {margin:0cm 0cm 0.0001pt; text-align:justify; font-size:12pt; font-family:cambria;} P.msolistparagraph, Li. Msolistparagraph, Div. msolistparagraph {margin:0cm 0cm 0.0001pt; text-align:justify; text-indent:21pt; font-size:12pt; font-family:cambria ; }. Msochpdefault {Font-family:cambria;} Div. WordSection1 {page:wordsection1;} OL {margin-bottom:0cm;} UL {margin-bottom:0cm;}

1. The underlying implementation of multithreading?

1> first figuring out what a thread is, what a multithreaded

2> Mach is the first system to handle tasks in a multi-threaded manner, so the underlying implementation mechanism for multithreading is Mach-based threading

Mach-Class threads are seldom used in 3> development, because Mach-level threads do not provide the basic features of multithreading, and the threads are independent

Implementation of multi-threading scheme in 4> development

POSIX interface for the C language: #include <pthread.h>

L OC's Nsthread

L C GCD Interface (best performance, more streamlined code)

L OC Nsoperation and Nsoperationqueue (based on GCD)

2. How do I communicate between threads?

1> PerformSelector:onThread:withObject:waitUntilDone:

2> Nsmachport

3. How to solve the problem of a duplicate request of the same network address in the network picture processing problem?

Use the dictionary (the image address is key, the download operation is value)

4. With nsopertion and nsopertionqueue processing a,b,c three threads, required to execute a, a, b before executing C, how to do?

Create a queue

Nsoperationqueue *queue = [[Nsoperationqueue alloc] init];

Create 3 actions

Nsoperation *a = [Nsblockoperation blockoperationwithblock:^{

NSLog (@ "Operation1---");

}];

Nsoperation *b = [Nsblockoperation blockoperationwithblock:^{

NSLog (@ "Operation1---");

}];

Nsoperation *c = [Nsblockoperation blockoperationwithblock:^{

NSLog (@ "Operation1---");

}];

Add dependency

[C Adddependency:a];

[C ADDDEPENDENCY:B];

Perform actions

[Queue addoperation:a];

[Queue addoperation:b];

[Queue addoperation:c];

5. The implementation of several multithreading in cocoa is enumerated, and several methods of multithreading security are discussed and how to control multithreading security.

1> only refreshes the access UI in the main thread

2> If you want to prevent resource looting, you have to use synchronized for lock protection

3> if the asynchronous operation to ensure the line Cheng and other issues, try to use GCD (some functions are safe by default)

6. How does the GCD interior come true?

The core of 1> iOS and OS X is the XNU kernel, GCD is implemented based on the XNU kernel

2> GCD APIs are all in the Libdispatch library

The underlying implementations of the 3> GCD are mainly dispatch queue and dispatch Source

L Dispatch Queue: Manage Block (operation)

L Dispatch Source: Handling Events

7. Have you ever used nsoperationqueue? If used or understood, why do you use Nsoperationqueue to achieve what? Please describe the differences and similarities between it and GCD (hint: it can be described from the implementation mechanism and scope of application).

1> GCD is a pure C language Api,nsoperationqueue is based on the GCD OC version package

2> GCD only supports FIFO queues, Nsoperationqueue can easily adjust the order of execution, set the maximum number of concurrent

3> Nsoperationqueue can easily set dependencies between operation, and GCD need to write a lot of code to achieve

4> nsoperationqueue Support KVO, you can monitor whether operation is executing (isexecuted), whether it ends (isfinished), whether it is canceled (Iscanceld)

5> GCD executes faster than Nsoperationqueue.

Tasks do not depend too much on each other: GCD

dependencies between tasks \ or to monitor the execution of a task: Nsoperationqueue

8. Since the mention of GCD, then ask what to pay attention to when using GCD and block? Are they two a thing? What is the difference between block behavior and usage in arc and the traditional MRC, and what do you need to be aware of?

Use of block Note:

1> Block's memory management

2> Prevention of cyclic Retian

L Non-arc (MRC): __block

L arc:__weak\__unsafe_unretained

9. Download a lot of pictures in an asynchronous thread, what if it fails? Please combine runloop to talk about the solution. (Hint: Start a runloop in an asynchronous thread to resend the network request, download the picture)

1> re-download pictures

2> download complete, use Runloop input source to return to the main thread refresh Uiimageviuew

How the socket is implemented and how the socket communicates

One. Implementation of the HTTP protocol

Interview question 08-multithreaded Network

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.