資源簡(jiǎn)介
用c#寫(xiě)的多線程求PI 比較簡(jiǎn)單的小程序 大家看看

代碼片段和文件信息
using?System;
using?System.Collections.Generic;
using?System.Linq;
using?System.Text;
using?System.Threading;
namespace?ListSample
{
????class?Program
????{
????????static?List?list?=?new?List();//供多個(gè)線程同時(shí)操作的單鏈表對(duì)象
????????//Main函數(shù)中演示是三個(gè)線程同時(shí)對(duì)該單鏈表執(zhí)行一系列操作得到的結(jié)果
????????static?void?Main(string[]?args)
????????{
????????????Thread?one?=?new?Thread(operation_sequence_one);
????????????Thread?two?=?new?Thread(operation_sequence_two);
????????????Thread?three?=?new?Thread(operation_sequence_three);
????????????one.Start();
????????????two.Start();
????????????three.Start();
????????????//-------------------
????????????one.Join();
????????????two.Join();
????????????three.Join();
????????????//輸出三個(gè)線程并發(fā)操作后鏈表元素
????????????list.printList();
????????}
????????//操作序列一
????????static?void?operation_sequence_one()
????????{
????????????list.insert(7);
????????????list.insert(9);
????????????list.insert(12);
????????????list.insert(6);
????????????list.delete(9);
????????}
????????//操作序列二
????????static?void?operation_sequence_two()
????????{
????????????list.insert(10);
????????????list.insert(5);
????????????list.delete(7);
????????}
????????//操作序列三
????????static?void?operation_sequence_three()
????????{
????????????list.insert(8);
????????????list.insert(4);
????????????list.delete(3);
????????????list.delete(4);
????????????list.delete(5);
????????}
????}
????//List是一個(gè)按鍵值非遞減順序排序的單鏈表類(lèi)并假設(shè)節(jié)點(diǎn)的鍵值都為正數(shù)
????class?List
????{
????????public?class?Node
????????{
????????????public?int?key;
????????????public?Node?next;
????????????public?Node()
????????????{
????????????????key?=?-1;
????????????????next?=?null;
????????????}
????????????public?Node(int?k)
????????????{
????????????????key?=?k;
????????????????next?=?null;
????????????}
????????}
????????Node?head;//頭節(jié)點(diǎn),作為哨兵節(jié)點(diǎn)存在
????????public?object?SynRoot;//同步對(duì)象,整個(gè)鏈表使用這一個(gè)同步對(duì)象
????????public?List()
????????{
????????????head?=?new?Node(-1);//新建頭節(jié)點(diǎn),這是一個(gè)哨兵節(jié)點(diǎn),永遠(yuǎn)不會(huì)被刪除
????????????SynRoot?=?new?object();//新建同步對(duì)象
????????}
????????//查找單鏈表中是否存在具有鍵值key的節(jié)點(diǎn)
????????public?bool?find(int?key)
????????{
????????????
????????????{
????????????????Node?pre?=?new?Node();
????????????????Node?cur?=?new?Node();
????????????????return?search(key?out?pre?out?cur);
????????????}
????????????
????????}
????????//向鏈表中插入一個(gè)鍵值為key的新節(jié)點(diǎn)
????????public?bool?insert(int?key)
????????{
???????????
????????????????Monitor.Enter(this);?
????????????????Node?pre?=?new?Node();
????????????????Node?cur?=?new?Node();
????????????????if?(search(key?out?pre?out?cur)?==?true)
????????????????{
????????????????????//具有該鍵值的節(jié)點(diǎn)已經(jīng)存在,插入失敗
????????????????????Monitor.Exit(this);
????????????????????return?false;
????????????????}
????????????????Node?newNode?=?new?Node(key);//新建節(jié)點(diǎn)并插入到鏈表中
????????????????newNode.next?=?cur;
????????????????pre.next?=?newNode;
????????????????Monitor.Exit(this);
????????????????return?true;
????????????
?
?屬性????????????大小?????日期????時(shí)間???名稱(chēng)
-----------?---------??----------?-----??----
?????文件???????6144??2010-01-04?22:16??thread\thread\bin\Debug\thread.exe
?????文件??????17920??2010-01-04?22:16??thread\thread\bin\Debug\thread.pdb
?????文件??????14328??2010-01-08?09:20??thread\thread\bin\Debug\thread.vshost.exe
?????文件????????490??2009-06-11?05:14??thread\thread\bin\Debug\thread.vshost.exe.manifest
?????文件????????295??2010-01-04?22:16??thread\thread\obj\Debug\thread.csproj.FileListAbsolute.txt
?????文件???????6144??2010-01-04?22:16??thread\thread\obj\Debug\thread.exe
?????文件??????17920??2010-01-04?22:16??thread\thread\obj\Debug\thread.pdb
?????文件????????110??2010-01-08?09:20??thread\thread\obj\Debug\thread1.csproj.FileListAbsolute.txt
?????文件???????5482??2010-01-04?22:16??thread\thread\Program.cs
?????文件???????1344??2010-01-04?21:15??thread\thread\Properties\AssemblyInfo.cs
?????文件???????2485??2010-01-04?21:15??thread\thread\thread1.csproj
?????文件????????910??2010-01-04?22:19??thread\thread.sln
????..A..H.?????11264??2010-01-08?09:21??thread\thread.suo
?????目錄??????????0??2010-01-04?21:15??thread\thread\obj\Debug\TempPE
?????目錄??????????0??2010-01-04?21:55??thread\thread\bin\Debug
?????目錄??????????0??2010-01-05?12:57??thread\thread\obj\Debug
?????目錄??????????0??2010-01-04?21:15??thread\thread\bin
?????目錄??????????0??2010-01-04?21:15??thread\thread\obj
?????目錄??????????0??2010-01-04?21:15??thread\thread\Properties
?????目錄??????????0??2010-01-04?22:17??thread\thread
?????目錄??????????0??2010-01-04?21:15??thread
-----------?---------??----------?-----??----
????????????????84836????????????????????21
評(píng)論
共有 條評(píng)論