一个多线程的练习

这篇博客通过一个实例展示了如何使用C#进行多线程操作。在`TestMultithread`命名空间中,定义了`MainThread`类作为主程序,它启动多个线程来并行处理任务。`TestQueue`类作为工作线程,负责从目标列表中取出数据进行处理,并将结果存入结果列表。`TestManager`类创建并管理这些工作线程,使用`ThreadPool`调度任务,并通过`ManualResetEvent`同步信号来等待所有线程完成。
摘要由CSDN通过智能技术生成
using  System;
using  System.Threading;
using  System.Collections;

namespace  TestMultithread
{
    
/// <summary>
    
/// Class1 的摘要说明。
    
/// </summary>

    class MainThread
    
{
        
internal static ArrayList Target;
        
internal static ArrayList Result;
        
/// <summary>
        
/// 应用程序的主入口点。
        
/// </summary>

        [STAThread]
        
static void Main(string[] args)
        
{
            ArrayList lista 
= new ArrayList();
            
for(int i=0;i<=100;i++)
            
{
                lista.Add(i);
            }

            TestManager m1 
= new TestManager();
            ArrayList result 
= m1.Test(lista);
            
foreach(int item in result)
            
{
                Console.WriteLine(item);
            }

            Console.ReadLine();
        }

    }


    
/// <summary>
    
/// 其中的doTest方法将为多线程执行的实体
    
/// </summary>

    public class TestQueue
    
{
        
private ArrayList target;
        
private ArrayList result;
        
private bool isFinished;
        
private int targetQueuePosition;
        
private ManualResetEvent eventX;
        
        
public TestQueue(ArrayList toBeTest,ManualResetEvent reseter)
        
{
            
this.Target = toBeTest;
            
this.Result = new ArrayList();
            
this.isFinished = false;
            
this.TargetQueuePosition = 0;
            
this.EventX = reseter;
        }


        
public ArrayList Target
        
{
            
get{return target;}
            
set{target = value;}
        }


        
public ArrayList Result
        
{
            
get{return result;}
            
set{result = value;}
        }


        
public bool IsFinished
        
{
            
get{return isFinished;}
            
set{isFinished = value;}
        }


        
public int TargetQueuePosition
        
{
            
get{return targetQueuePosition;}
            
set{targetQueuePosition = value;}
        }


        
public ManualResetEvent EventX
        
{
            
get{return eventX;}
            
set{eventX = value;}
        }

        
        
public ArrayList GetResult()
        
{
            
return this.Result;
        }

        
public void DoTest(object State)
        
{
            
while(true)
            
{
                Console.WriteLine(
"threadHashCode: {0} do some work",Thread.CurrentThread.GetHashCode());


                
if(this.isFinished)
                
{
                    
return;
                }

                
int tempData;
                
lock(this)
                
{
                    
if(this.TargetQueuePosition >= this.Target.Count)
                    
{
                        
return;
                    }

                    
else
                    
{
                        tempData 
= Convert.ToInt32(this.Target[this.TargetQueuePosition]);
                        
this.TargetQueuePosition++;
                    }

                }

                Thread.Sleep(
100);
                
lock(this)
                
{
                    
this.Result.Add(tempData);
                    
if(this.Result.Count == this.Target.Count)
                    
{
                        
this.isFinished = true;
                        
this.EventX.Set();
                    }

                }

            }

        }

    }


    
public class TestManager
    
{
        
public ArrayList Test(ArrayList toBeTest)
        
{
            ManualResetEvent eventX 
= new ManualResetEvent(false);
            TestQueue queue 
= new TestQueue(toBeTest,eventX);
            
int maxCount = 3;
            WaitCallback callback 
= new WaitCallback(queue.DoTest);
            
for(int i=0; i<maxCount; i++)
            
{
                ThreadPool.QueueUserWorkItem(callback);
            }

            eventX.WaitOne(Timeout.Infinite,
true);
            
return queue.GetResult();
        }

    }

}

 
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值