下令执行线程 [英] Ordered execute threads

查看:129
本文介绍了下令执行线程的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个互动的另一个用户界面的控制台应用程序。接口发送命令和我的应用程序应该对它们进行处理。重要的是要继续听我的控制台应用程序在处理和有序的执行命令是非常重要的。所以,我听上主线程接口,而在另一个线程中执行命令。

例如下面是什么,我想和问题是执行的线程不排序。

第二件事是我使用锁ProcessCommand方法,但我不知道它安全与否。例如,一个以上的线程可以ProcessCommand方法里面这样之一是过程锁,以便其他线程可以改变传入输入值。我对吗 ?我做了一些测试,并从未发生过,但仍怀疑它。

有什么可以最好的解决办法?解决方案可以线程与否线程。

 类节目
{
    静态对象更衣室=新的对象();
    静态无效的主要(字串[] args)
    {
        而(真)
        {
            VAR输入=到Console.ReadLine();
            (新主题(新ParameterizedThreadStart(ProcessCommand)))启动(输入)。
        }
        Console.ReadKey();
    }
    静态无效ProcessCommand(动态输入)
    {
        锁(更衣室)
        {
            Console.WriteLine(输入);
            //处理传入的命令
        }
    }
}
 

解决方案

您已经开始输入的每行一个新的线程。这就是为什么你不能保证订货。

相反的是,创建一个其中的阅读器可以把线到队列中,处理器可以读出。有一个线程读取和一个线程处理。我有在我的线程教程一些示例code 以生产者/消费者队列,虽然这是非常简单的。

如果您使用的是.NET 4(我猜你是给出了使用动态),然后并行扩展使得这个的的容易使用 BlockingCollection< T> IProducerConsumerCollection< T> 。今天是你的幸运日 - 乔阿尔巴哈利取得了href="http://www.albahari.com/threading/part5.aspx" rel="nofollow">并行扩展的C#4的一部分果壳中的公众对他的网站。通读这一点,就应该让一切更加清晰。你可能想直接跳到 BlockingCollection< T> 部分,但是这一切都值得一读。

I have an console application which interact another user interface. Interface sends commands and my application should to process them. It is important to keep listening my console application while processing and execute commands in ordered. So i listen interface on main thread and execute commands in another thread.

Below example is what i am trying and problem is execution threads are not ordered.

Second thing is i am using lock in ProcessCommand method but i am not sure it is safe or not. For example one more threads can be inside of ProcessCommand method so one was process in lock so other thread can change incoming input value. Am i right ? I did some test and that never happen but still suspicious about it.

What can be best solution ? Solution Can be threading or not threading.

class Program
{
    static object locker = new object();
    static void Main(string[] args)
    {
        while (true)
        {
            var input = Console.ReadLine();
            (new Thread(new ParameterizedThreadStart(ProcessCommand))).Start(input);
        }
        Console.ReadKey();
    }
    static void ProcessCommand(dynamic input)
    {
        lock (locker)
        {
            Console.WriteLine(input);
            //process incoming command
        }
    }
}

解决方案

You're starting a new thread for each line of input. That's why you can't guarantee the ordering.

Instead of that, create a queue which the "reader" can put lines into, and the "processor" can read from. Have one thread reading and one thread processing. I have some sample code in my threading tutorial for a producer/consumer queue, although it's very simplistic.

If you're using .NET 4 (which I guess you are given the use of dynamic) then Parallel Extensions makes this much easier using BlockingCollection<T> and IProducerConsumerCollection<T>. Today is your lucky day - Joe Albahari has made the Parallel Extensions part of C# 4 in a Nutshell public on his web site. Read through that and it should make everything much clearer. You may want to skip to the BlockingCollection<T> part, but it's all worth reading.

这篇关于下令执行线程的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆