Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/multithreading/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/csharp-4.0/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Multithreading 触发多个同步线程_Multithreading_C# 4.0 - Fatal编程技术网

Multithreading 触发多个同步线程

Multithreading 触发多个同步线程,multithreading,c#-4.0,Multithreading,C# 4.0,我不确定这是否是一个愚蠢的问题,因为我对线程了解不多,但是否有可能同时启动多个同步线程,并等待所有线程完成后再采取行动?如果是这样的话,你怎么做?你不能同时做任何事情,更不用说触发线程了:)(你可以一个接一个地快速触发线程,尽管有可能在触发最后一个线程之前启动一个线程) 至于在继续之前等待所有线程,您可以使用Join方法,该方法在继续之前等待线程结束。您不能同时执行任何操作,更不用说触发线程:)(您可以一个接一个地快速触发线程,尽管有可能在触发最后一个线程之前启动线程) 至于在继续之前等待它们,

我不确定这是否是一个愚蠢的问题,因为我对线程了解不多,但是否有可能同时启动多个同步线程,并等待所有线程完成后再采取行动?如果是这样的话,你怎么做?

你不能同时做任何事情,更不用说触发线程了:)(你可以一个接一个地快速触发线程,尽管有可能在触发最后一个线程之前启动一个线程)

至于在继续之前等待所有线程,您可以使用Join方法,该方法在继续之前等待线程结束。

您不能同时执行任何操作,更不用说触发线程:)(您可以一个接一个地快速触发线程,尽管有可能在触发最后一个线程之前启动线程)

至于在继续之前等待它们,您可以使用Join方法,它在继续之前等待线程结束

ut是否可以同时启动多个同步线程,并等待所有线程完成后再执行操作

“同步线程”是一个矛盾修饰法,它们并不存在

当然,您可以启动多个线程并等待它们完成(
Thread.Join(otherThread)

如果是,你怎么做

很少。始终使用尽可能少的线程。它们很贵

确保您了解线程池和(Fx4)任务库TPL

ut是否可以同时启动多个同步线程,并等待所有线程完成后再执行操作

“同步线程”是一个矛盾修饰法,它们并不存在

当然,您可以启动多个线程并等待它们完成(
Thread.Join(otherThread)

如果是,你怎么做

很少。始终使用尽可能少的线程。它们很贵


确保您了解线程池和(Fx4)任务库TPL当然最简单的方法是使用.NET 4.0的任务并行库(TPL)

e、 g


请参阅:

当然,最简单的方法是使用.NET4.0的任务并行库(TPL)

e、 g

请参阅:

您可以使用。
这将并行执行提供的操作,并在所有操作完成后返回。

您可以使用。

这将并行执行所提供的操作,并在所有操作完成后返回。

通常您会使用如下构造

public class MultipleThreqadTest
{
    private readonly Thread[] threads;
    private readonly object locker;
    private int finishCounter;
    private readonly AutoResetEvent waitEvent;

    public MultipleThreqadTest()
    {
        threads=new Thread[10];
        for(int i=0;i<0;i++)
            threads[i]=new Thread(DoWork);
        finishCounter = threads.Length;
        waitEvent=new AutoResetEvent(false);

    }
    public void StartAll()
    {
        foreach (var thread in threads)
        {
            thread.Start();
        }
        //now wait for all worker threads to complete
        waitEvent.WaitOne();
    }


    private void DoWork()
    {
        //Do Some Actual work here, you may need to lock this in case you are workin on some shared resource
        //lock(locker)
        //{

        //}

        //Check if all worker thread complets
        if(Interlocked.Decrement(ref finishCounter)==0)
        {
            this.waitEvent.Set();
        }
    }

}
公共类MultipleThreqadTest
{
私有只读线程[]线程;
私有只读对象锁;
私人整饰柜台;
私有只读自动resetEvent waitEvent;
公共MultipleThreqadTest()
{
螺纹=新螺纹[10];

对于(inti=0;i,通常使用如下构造

public class MultipleThreqadTest
{
    private readonly Thread[] threads;
    private readonly object locker;
    private int finishCounter;
    private readonly AutoResetEvent waitEvent;

    public MultipleThreqadTest()
    {
        threads=new Thread[10];
        for(int i=0;i<0;i++)
            threads[i]=new Thread(DoWork);
        finishCounter = threads.Length;
        waitEvent=new AutoResetEvent(false);

    }
    public void StartAll()
    {
        foreach (var thread in threads)
        {
            thread.Start();
        }
        //now wait for all worker threads to complete
        waitEvent.WaitOne();
    }


    private void DoWork()
    {
        //Do Some Actual work here, you may need to lock this in case you are workin on some shared resource
        //lock(locker)
        //{

        //}

        //Check if all worker thread complets
        if(Interlocked.Decrement(ref finishCounter)==0)
        {
            this.waitEvent.Set();
        }
    }

}
公共类MultipleThreqadTest
{
私有只读线程[]线程;
私有只读对象锁;
私人整饰柜台;
私有只读自动resetEvent waitEvent;
公共MultipleThreqadTest()
{
螺纹=新螺纹[10];

对于(int i=0;ithanks,我对性能不太在意,这是一种罕见的现象,我这样做的唯一原因是因为我必须一次多次调用一个web服务,我对性能不太在意,这是一种罕见的现象,我这样做的唯一原因是因为我必须一次多次调用一个web服务这听起来像是最简单的方法这听起来像是最简单的方法