c socket執行緒池實現

2021-08-15 05:36:45 字數 3676 閱讀 5582

伺服器端:

poolserver.cs類

using system;

using system.collections.generic;

using system.text;

using system.net;

using system.net.sockets;

using system.io;

using system.threading;

namespace net.common.pool

set

}///

/// 埠

///

private int port;

///

/// 埠

///

public int port

set

}///

/// 第幾張網絡卡,預設為第乙個,值為0

///

private int addressindex = 0;

///

/// 第幾張網絡卡,預設為第乙個,值為0

///

public int addressindex

set

}///

/// 是否開啟服務

///

private bool release = false;

///

/// 執行緒集合

///

private listlistconnectionthread = new list();

private tcplistener listener = null;

private tcpclient client = null;

///

/// 

///

public poolserver()

///

/// 

///

/// 埠

/// 最大活動數量

public poolserver(int port, int maxconnections)

///

/// 

///

/// 第幾張網絡卡

/// 埠

/// 最大活動數量

public poolserver(int addressindex, int port, int maxconnections)

///

/// 啟動服務

///

public void serverstart()

///

///  停止服務

///

public void serverstop()

//清空

listconnectionthread.clear();

}catch (exception e)}}

///

/// 允許客戶機連線到伺服器,等待客戶機請求

/// 1.開啟乙個執行緒

///

private void acceptconnections()

///

/// 允許客戶機連線到伺服器,等待客戶機請求

/// 2.判斷當前release(是否啟用服務)的值是否為true

/// 如果沒有啟用服務,則執行緒關閉

/// 如果啟用服務,則開始等待客戶機請求,如果受到了客戶機的請求,則加入池中

///

private void listenerstart()

catch (exception e)}}

///

/// 將請求加入池中

///

///

private void handleconnection(tcpclient client)

///

///  開啟pooledconnectionhandler執行緒

///

private void setuphandlers()}}

}poolconnectionhandler.cs類

using system;

using system.collections.generic;

using system.text;

using system.net;

using system.net.sockets;

using system.io;

using system.threading;

namespace net.common.pool

///

/// 業務處理

///

public void handleconnection()

catch 

}///

/// 將未處理的請求加入池中

///

///

public static void processrequest(tcpclient requesttohandle)

}///

///  一直接讀取pool(池),並且對pool進行加鎖,判斷其集合是否為空

/// 如果pool為空,則一直的讀取

/// 如要pool不為空,則取出第乙個值,到了這一步鎖解除(儲存池取值的過程是安全,取出的值是唯一),接著執行handleconnection

///

public void run()

//取第乙個值

client = pool.first.value;

//移除

pool.removefirst();

}handleconnection();

}catch (exception e)}}

}}handlemain.cs業務類:

using system;

using system.collections.generic;

using system.text;

using system.net;

using system.net.sockets;

using system.io;

namespace net.common.pool

filestream.flush();

}catch (exception ex)

finally

if (streamreader != null)

if (networkstream != null)

if (client != null)}}

}}執行伺服器端:

int port = 3848;

poolserver poolserver = new poolserver(port, 10);

poolserver.serverstart();                  

客戶端測試:

using system.net;

using system.io;

using system.net.sockets;

public void send()

networkstream.flush();

}catch (exception ex)

finally

if (streamwriter != null)

if (networkstream != null)

if (client != null)}}

}

實現執行緒池

1.執行緒池優點 1 減少建立和銷毀執行緒的次數 2 可以根據系統的能力,調整執行緒池中線程的數目 3 減少切換執行緒的開銷 2.實現自定義執行緒池 思路 public class threadpool extends threadgroup 加入任務 public synchronized voi...

c 執行緒池實現(四)執行緒池實現

前面已經說到了同步佇列的實現,下面來看執行緒池的實現。ifndef include threadpool define include threadpool include include include include include syncqueue.hpp namespace mythrea...

記憶體池 程序池 執行緒池介紹及執行緒池C 實現

平常我們使用new malloc在堆區申請一塊記憶體,但由於每次申請的記憶體大小不一樣就會產生很多記憶體碎片,造成不好管理與浪費的情況。記憶體池則是在真正使用記憶體之前,先申請分配一定數量的 大小相等 一般情況下 的記憶體塊留作備用。當有新的記憶體需求時,就從記憶體池中分出一部分記憶體塊,若記憶體塊...