常用到的六種建立多執行緒的方法

2021-06-26 09:51:17 字數 1205 閱讀 5601

nsthread *t = [[nsthread alloc] initwithtarget:self selector:@selector(mutablethread) object:nil];

方法二:

[nsthread detachnewthreadselector:@selector(mutablethread) totarget:self withobject:nil];

方法三:

[self performselectorinbackground:@selector(mutablethread) withobject:nil];

方法四:多執行緒blog建立

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

//會開啟乙個多執行緒

[operationqueue addoperationwithblock:^

}];方法五:

//相當於是乙個執行緒池

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

operationqueue.maxconcurrentoperationcount = 1;//設定併發數

//建立執行緒

nsinvocationoperation *opertion1 = [[nsinvocationoperation alloc] initwithtarget:self selector:@selector(thread1) object:nil];

//設定執行緒的優先順序

[opertion1 setqueuepriority:nsoperationqueuepriorityverylow];

//建立另乙個執行緒

nsinvocationoperation *opertion2 = [[nsinvocationoperation alloc] initwithtarget:self selector:@selector(thread2) object:nil];

[opertion2 setqueuepriority:nsoperationqueuepriorityhigh];

方法六:

dispatch_queue_t queue = dispatch_queue_create("test",null);

dispatch_async(queue,^);

JAVA多執行緒的六種狀態例項

多執行緒六種狀態例項,最下方圖是執行 流程,希望對你有幫助!多執行緒的六種狀態 public class threadstatustest catch interruptedexception e 同步鎖 synchronized obj catch interruptedexception e s...

多執行緒的六種實現方式 知識總結(二)

步驟 1.建立類並繼承thread類 2.重寫run方法 3.建立該類的例項 4.呼叫該類的strat 方法,直接呼叫run方法不會去開啟執行緒,只會以普通方法執行 步驟 1.建立類並實現runnable介面 2.重寫run方法 3.建立該類的例項 4.呼叫strat 方法 步驟 1.建立類並定義乙...

JAVA的六種排序方法

冒泡 publicstringbubble sort stringarr returnarr 選擇publicstringselect sort stringarr returnarr 插入 publicstringinsert sort stringarr else returnarr 希爾pub...