我有以下设置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);
}
}