多執行緒的技術總結

2021-06-26 12:59:23 字數 2006 閱讀 4528

多執行緒技術總結

performselectorinbackground:withobject:

nsthread

靜態方法

+ (void)detachnewthreadselector:(sel)selector totarget:(id)target withobject:(id)argument; 建立

nsthread

物件,然後呼叫

start

方法來啟動執行緒

//第⼀一種建立執行緒的⽅方法

[self performselectorinbackground:@selector(handletask)

withobject:nil];

//第⼆二種建立執行緒的⽅方法

withobject:nil];

第⼀乙個引數:執行緒要執⾏行的任務 第⼆二個引數:執行緒要執⾏行的任務在哪個物件⾥裡 第三個引數

//直接建立了⼀乙個執行緒, 建立之後, 執行緒直接啟動

[nsthread detachnewthreadselector:@selector(handletask) totarget:self

//第三種建立執行緒的⽅方法

selector:@selector(handletask) object:nil];

//2

、給執行緒起⼀乙個名字

mythread.name = @"qingyun";

//3、需要⼿手動啟動這個執行緒

[mythread start];

、建立⼀乙個執行緒物件

(nsthread)

nsthread *mythread = [[nsthread alloc ]initwithtarget:self

4. 建立

nsoperation

子類物件,然後使用

nsoperationqueue

來啟動執行緒

//第四種建立執行緒的⽅方法

initwithtarget:self selector:@selector(handletask) object:nil];

// 3

、將操作物件,新增到佇列⾥裡, 需要注意的是:新增佇列⾥裡之後,執行緒就開始啟動

[queue addoperation:operation];

、建立⼀乙個多執行緒的操作佇列

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

//2

、建立⼀乙個操作物件,這個操作物件主要功能是定義了執行緒需要執⾏行的執務

nsinvocationoperation *operation = [[nsinvocationoperation alloc]

5. 建立

nsblockopertation

物件,然後新增到

nsoperationquue

來啟動執行緒

第五種建立執行緒的⽅方法

// [nsthread sleepfortimeinterval:10];

if ([thread ismainthread]) 

nslog(@"handle:%ld",i);

先拿到⼀乙個執行緒物件

,返回的是表⽰示當前執行緒的執行緒物件

nsthread *thread = [nsthread currentthread];

通過執行緒物件的⽅方法來判斷是否是主線程, 如果是主線程則列印⼀一條是主線程的

nslog(@"main thread.");

nslog(@"done.");

、將nsblockoperation

物件新增到佇列⾥裡

多執行緒技術總結

1,程序和執行緒的概念。程序 執行緒 2,jvm中的多執行緒體現。主線程,垃圾 執行緒,自定義執行緒。以及他們執行的 的位置。3,什麼時候使用多執行緒,多執行緒的好處是什麼?建立執行緒的目的?當需要多部分 同時執行的時候,可以使用。4,建立執行緒的兩種方式。繼承thread 步驟 實現runnabl...

C 多執行緒技術總結(非同步)

我這裡針對現有的c 多執行緒技術進行乙個彙總,一是複習,二是方便索引,文章部份知識點 於網路,非本人原創。一 並行 非同步 1.system.threading.tasks命名空間下的 tpl 1.1 parallel.invoke 並行執行多個任務,主線程等待並行執行完畢後才開始續續執行。示例 s...

多執行緒技術

1,程序 執行緒 程序 系統中同時執行的不同程式 執行緒 程式中同時執行不同的操作 單個cpu只能按順序執行指令,cpu可以隨機在不同的程序和執行緒進行切換,保證程序和執行緒都執行一遍後再重複這個過程。因為cpu執行速度足夠快,讓人感覺程式是同時執行的。2,執行緒 thread thread sle...