c 多執行緒排隊佇列實現的原始碼

2021-05-27 19:43:14 字數 2020 閱讀 3626

using system;

using system.threading;

using system.collections;

using system.collections.generic;

// 將執行緒同步事件封裝在此類中,

// 以便於將這些事件傳遞給 consumer 和

// producer 類。

public class syncevents

// 公共屬性允許對事件進行安全訪問。

public eventwaithandle exitthreadevent

}public eventwaithandle newitemevent

}public waithandle eventarray

}private eventwaithandle _newitemevent;

private eventwaithandle _exitthreadevent;

private waithandle _eventarray;

}// producer 類(使用乙個輔助線程)

// 將項非同步新增到佇列中,共新增 20 個項。

public class producer

public void threadrun()}}

console.writeline("producer thread: produced items", count);

}private queue_queue;

private syncevents _syncevents;

}// consumer 類通過自己的輔助線程使用佇列

// 中的項。producer 類使用 newitemevent

// 將新項通知 consumer 類。

public class consumer

public void threadrun()

count++;

}console.writeline("consumer thread: consumed items", count);

}private queue_queue;

private syncevents _syncevents;

}public class threadsyncsample

", i);}}

console.writeline();

}static void main()

// 向使用者執行緒和製造者執行緒發出終止訊號。

// 這兩個執行緒都會響應,由於 exitthreadevent 是

// 手動重置的事件,因此除非顯式重置,否則將保持「設定」。

console.writeline("signaling threads to terminate...");

syncevents.exitthreadevent.set();

// 使用 join 阻塞主線程,首先阻塞到製造者執行緒

// 終止,然後阻塞到使用者執行緒終止。

console.writeline("main thread waiting for threads to finish...");

producerthread.join();

consumerthread.join();

}}

public delegate void delegate1();

public delegate void delegate2(datatable dt);

public void buttonfind_click(object sender, eventargs e)

public void asynccallback1(iasyncresult iasyncresult)

public void find());}

public void addrow(datatable dt, string name, int age)

public void bind2(datatable dt)

}}

c 多執行緒排隊佇列實現的原始碼

using system using system.threading using system.collections using system.collections.generic 將執行緒同步事件封裝在此類中,以便於將這些事件傳遞給 consumer 和 producer 類。public ...

c 多執行緒 原始碼3

在開發中經常會遇到執行緒的例子,如果某個後台操作比較費時間,我們就可以啟動乙個執行緒去執行那個費時的操作,同時程式繼續執行。在某些情況下可能會出現多個執行緒的同步協同的問題,下面的例子就展示了在兩個執行緒之間如何協同工作。這個程式的思路是共同做一件事情 從乙個arraylist中刪除元素 如果執行完...

c 多執行緒 原始碼5

c thread.join 方法阻塞呼叫執行緒,直到某個執行緒終止時為止 我們可以這麼理解 當newthread呼叫join方法的時候,mainthread就被停止執行,直到newthread執行緒執行完畢 using system using system.collections.generic ...