• 大小: 24KB
    文件类型: .rar
    金币: 1
    下载: 0 次
    发布日期: 2021-06-03
  • 语言: C#
  • 标签: PI  多线程  c#  

资源简介

用c#写的多线程求PI 比较简单的小程序 大家看看

资源截图

代码片段和文件信息

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;

namespace ListSample
{
    class Program
    {
        static List list = new List();//供多个线程同时操作的单链表对象

        //Main函数中演示是三个线程同时对该单链表执行一系列操作得到的结果
        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();

            //输出三个线程并发操作后链表元素
            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是一个按键值非递减顺序排序的单链表类并假设节点的键值都为正数
    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;//头节点,作为哨兵节点存在
        public object SynRoot;//同步对象,整个链表使用这一个同步对象

        public List()
        {
            head = new Node(-1);//新建头节点,这是一个哨兵节点,永远不会被删除
            SynRoot = new object();//新建同步对象
        }

        //查找单链表中是否存在具有键值key的节点
        public bool find(int key)
        {
            
            {
                Node pre = new Node();
                Node cur = new Node();
                return search(key out pre out cur);
            }
            
        }

        //向链表中插入一个键值为key的新节点
        public bool insert(int key)
        {
           
                Monitor.Enter(this); 
                Node pre = new Node();
                Node cur = new Node();
                if (search(key out pre out cur) == true)
                {
                    //具有该键值的节点已经存在,插入失败
                    Monitor.Exit(this);
                    return false;
                }
                Node newNode = new Node(key);//新建节点并插入到链表中
                newNode.next = cur;
                pre.next = newNode;
                Monitor.Exit(this);
                return true;
            
 

 属性            大小     日期    时间   名称
----------- ---------  ---------- -----  ----

     文件       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


评论

共有 条评论