建立自定義執行緒池

2021-09-07 22:32:28 字數 3740 閱讀 5446

1. 建立乙個單例模式的自定義執行緒池類

public

class

customthreadpool

public

static

customthreadpool instance

}//#endregion

private

void

initializethreadpool()

}

2. 定義一些基本型別來和執行緒池進行通訊

public

delegate

void

usertask();

public

class

clienthandle

public

class

taskstatus

看到上面的**了嗎?usertask是乙個**,代表了執行緒池中線程將要執行的任務。

3. 接下來我們給我們的自定義執行緒池類增加一些公共介面的方法

public clienthandle queueusertask(usertask task, actioncallback)

public

static

void

cancelusertask(clienthandle handle)

4. 下面是執行緒池需要使用的一些內部類和型別

//

#region nested private types

enum taskstate //

to represent current state of a usertask

class taskhandle //

item in waiting queue

class taskitem //

running items in the pool - taskhandle gets a thread to execute it

//#endregion

5. 下面我們需要做的是執行緒池的初始化工作,並且初始化任務佇列

//

private instance members

private queuereadyqueue = null

;private listpool = null

;private thread taskscheduler = null

;private

void

initializethreadpool()

); taskscheduler.start();

}

上面需要特別注意的是,taskscheduler這個執行緒類物件。

這是始終貫穿執行緒池生命週期的乙個額外的執行緒,也可以說是主線程。

它的任務是監視使用者任務佇列,並且盡快地把它們帶去執行,另外它還負責強制最大和最小

執行緒數的限制,做一些清理工作。

6. 接著就是實現執行緒初始化了,我們使用的是就近完成演算法

private

void

initializethreadpool()

}if (!added && pool.count ;

ti.taskhandle =readyitem;

//add a new taskitem in the pool

addtasktopool(ti);

added = true

; readyqueue.dequeue();

}if (!added) break; //

it's already crowded so try after sometime

}

if ((datetime.now - lastcleanup) >timespan.frommilliseconds(cleanup_interval))

//it's long time - so try to cleanup pool once.

else

} while (true

); });

taskscheduler.priority =threadpriority.abovenormal;

taskscheduler.start();

}private

void

initpoolwithmincapacity()

; ti.taskhandle = new taskhandle() };

ti.taskhandle.callback = (taskstatus) =>;

ti.taskhandle.token = new clienthandle() ;

addtasktopool(ti);

}}private

void

addtasktopool(taskitem taskitem)

if(enter)

catch

(exception ex)

if (taskitem.taskhandle.callback != null && taskitem.taskstate !=taskstate.aborted)

catch}}

//give other thread a chance to execute as it's current execution completed already

thread.yield(); thread.sleep(min_wait); //

todo: need to see if sleep is required here

} while (true); //

it's a continuous loop until task gets abort request

}); taskitem.handler.start();

pool.add(taskitem);

}private

void

cleanuppool()

7. 使用者任務佇列實現

public clienthandle queueusertask(usertask task, actioncallback)

, callback =callback

};readyqueue.enqueue(th);

return

th.token;

}

8. 最後便是測試**

customthreadpool mypool;

private

void form1_load(object

sender, eventargs e)

void showmessage(string

message)

int x = 0

;private

void btnstart_click(object

sender, eventargs e)

, (ts) =>);

}

自定義執行緒池,如何最佳建立執行緒池

j a有預置執行緒池 newsinglethreadexecutor,newfixedthreadpool,newcacheedthreadpool,newscheduledthreadpool,newworkstealingpool。如果不適合,還可以使用threadpoolexecutor建立自...

自定義執行緒池

有些時候 jdk自帶的cachedthreadpool fixedthreadpool等執行緒池完成不了我們業務的需求時 可以用threadpoolexecutorg構造自定義的執行緒池。public class usethreadpoolexecutor1 這段 會首先執行任務1,然後把2 3 4...

自定義執行緒池

建立執行緒池方法 儘管executors提供了四種執行緒池建立的方式,但為了實現某些特定的需求,可以自己建立執行緒池。如在阿里的程式設計規範使用executors建立執行緒時,一般會報錯,並提示以下資訊 執行緒池不允許使用executors去建立,而是通過threadpoolexecutor的方式,...