0

我有以下设置TransformBlock

private void SetupTestModule()
{
    Func<int, int> func1 = new Func<int, int>(input =>
        {
            return (input + 1);
        });

    Func<int, int> func2 = new Func<int, int>(input =>
        {
            return (input + 2);
        });

    TransformBlock<int, int> transform = new TransformBlock<int, int>(func1);
}

我想知道我是否可以在运行时将 func1 换成 func2,以及这个 transformBlock 和其他数据块之间的所有链接是否会保持不变?理想情况下,在交换功能时,我只是希望新的转换适用于所有新传入的项目。显然,通过这种简单化的方法,我不会对当前在队列中的项目以及如何处理它们做出假设。我只是想知道分配一个新的 Func 是否会导致运行时错误或导致 transformBlock 被取消链接。谁能分享一些见解?

编辑:在这里发布 Jon 的建议和代码一些非常基本的测试代码。让我好奇的是,它可以在有无volatile关键字的情况下使用。为什么我们需要volatile

public class TransformBlockHotSwap
{
    private TransformBlock<int, int> transformBlock;
    private ActionBlock<int> actionBlock;

    public TransformBlockHotSwap()
    {

        SwappableFunction<int, int> swappable = new SwappableFunction<int, int>(item => item + 1);
        transformBlock = new TransformBlock<int, int>(item => swappable.Execute(item));
        actionBlock = new ActionBlock<int>(item => Console.WriteLine(item));
        transformBlock.LinkTo(actionBlock);

        Func<int, int> func2 = new Func<int,int>(item => item * item);

        for (int index = 1; index <= 100; index++)
        {
            transformBlock.Post(index);

            Thread.Sleep(500);

            if (index == 5)
            {
                swappable.Swap(func2);
            }
        }
    }
}

public class SwappableFunction<TInput, TOutput>
{
    private Func<TInput, TOutput> func;

    public SwappableFunction(Func<TInput, TOutput> func)
    {
        this.func = func;
    }

    public void Swap(Func<TInput, TOutput> newFunc)
    {
        func = newFunc;
    }

    public TOutput Execute(TInput input)
    {
        return func(input);
    }
}

编辑(包括谓词交换):

public class TransformBlockHotSwap
{
    private TransformBlock<int, int> transformBlock;
    private ActionBlock<int> actionBlock;

    public TransformBlockHotSwap()
    {
        Func<int, int> defaultFunction = new Func<int, int>(item => item);
        Func<int, int> func2 = new Func<int, int>(item => item * item);
        Predicate<int> defaultPredicate = new Predicate<int>(item => true);
        Predicate<int> pred2 = new Predicate<int>(item =>
            {
                if (item % 2 == 0)
                {
                    return true;
                }
                else
                {
                    return false;
                }
            });

        SwappableFunction<int, int> swappableFunction = new SwappableFunction<int, int>(defaultFunction);
        SwappablePredicate<int> swappablePredicate = new SwappablePredicate<int>(defaultPredicate);

        transformBlock = new TransformBlock<int, int>(item => swappableFunction.Execute(item));
        actionBlock = new ActionBlock<int>(item => Console.WriteLine(item));
        transformBlock.LinkTo(actionBlock, item => swappablePredicate.Execute(item));

        for (int index = 1; index <= 100; index++)
        {
            transformBlock.Post(index);

            if (index == 10)
            {
                swappablePredicate.Swap(pred2);
            }

            Thread.Sleep(200);
        }

        Console.WriteLine("Done");
        Console.ReadKey();
    }
}

public class SwappableFunction<TInput, TOutput>
{
    private volatile Func<TInput, TOutput> func;

    public SwappableFunction(Func<TInput, TOutput> defaultFunction)
    {
        this.func = defaultFunction;
    }

    public void Swap(Func<TInput, TOutput> newFunc)
    {
        func = newFunc;
    }

    public TOutput Execute(TInput input)
    {
        return func(input);
    }
}

public class SwappablePredicate<TInput>
{
    private volatile Predicate<TInput> predicate;

    public SwappablePredicate(Predicate<TInput> defaultPredicate)
    {
        this.predicate = defaultPredicate;
    }

    public void Swap(Predicate<TInput> newPredicate)
    {
        predicate = newPredicate;
    }

    public bool Execute(TInput input)
    {
        return predicate(input);
    }
}
4

1 回答 1

3

我不希望您能够这样做 - 但您可以轻松编写一个委托的函数:

public class SwappableFunction<TInput, TOutput>
{
    private volatile Func<TInput, TOutput> func;

    public SwappableFunction(Func<TInput, TOutput> func)
    {
        this.func = func;
    }

    public void Swap(Func<TInput, TOutput> newFunc)
    {
        func = newFunc;
    }

    public TOutput Execute(TInput input)
    {
        return func(input);
    }
}

然后:

var swappable = new SwappableFunction<int, int>(input => input + 1);
var block = new TransformBlock<int, int>(swappable.Execute);
// Later...

swappable.Swap(input => input + 2);

重要- 我不能 100% 确定volatile这里的使用 - 我通常不喜欢使用volatile,因为它的语义令人困惑。使用Interlocked.CompareExchange可能会更好 - 但代码会明显更长,我想先了解重点:)

于 2013-02-16T08:59:01.150 回答