特别是
创建一个函数以数组和索引作为参数。
创建一个元素数组。
创建一个计数循环。
在新线程的循环内部,使用传入的索引器将对象的新实例分配给数组。
我知道如何管理线程等。我有兴趣知道这是否是线程安全的方法。
class Program
{
// bogus object
class SomeObject
{
private int value1;
private int value2;
public SomeObject(int value1, int value2)
{
this.value1 = value1;
this.value2 = value2;
}
}
static void Main(string[] args)
{
var s = new SomeObject[10];
var threads = Environment.ProcessorCount - 1;
var stp = new SmartThreadPool(1000, threads, threads);
for (var i = 0; i < 10; i++)
{
stp.QueueWorkItem(CreateElement, s, i);
}
}
static void CreateElement(SomeObject[] s, int index)
{
s[index] = new SomeObject(index, 2);
}
}
浮云间
暮色呼如
一只名叫tom的猫
相关分类