在这个多线程程序中,我试图在填充它的第三次迭代中从列表中删除两个随机元素。我想我可以通过计算“stepCounter”的模数来跟踪新元素被添加到列表中的次数(所有线程执行相同的功能),并在检查几个断点后,我发现它正在检查stepCounter条件,然后转到“for”循环的第一行,选择要删除的两个随机索引。
问题是实际上并没有执行删除,就像它读取“for i = etc.然后只是跳过。
命名空间ThreadWinApp { 公共部分类Form1:表格 { //这里为整个类创建了线程变量 private Thread trd; private thread trd2; 私有线程trd3;
//this locker object is used to sychronize the threads
private System.Object locker = new System.Object();
//random value object for use in all threads
Random random = new Random();
public int val;
private int stepCounter = 0;
//our "array" of integers undefined in size
private List<int> numList = new List<int>();
private int[] arry;
private void ThreadTask()
{
//this the thread that will at random intervals add a random number to the collection.
// Every third iteration, the thread should remove two random
// elements from the collection.
int randomVal;
int randomListVal;
// int stepCounter = 0;
while (true)
{
lock (locker)
{
randomVal = random.Next(0, 20);
numList.Add(randomVal);
stepCounter += 1;
if (((stepCounter % 3) == 0))
for (int i = 0; i == 2; i++)
{
randomListVal = random.Next(0, numList.Count);
numList.RemoveAt(randomListVal);
}
Thread.Sleep(randomVal * 100);
}
}
}
private void ThreadTask2()
{
//this the thread that will read and show data from the array created in ThreadTask2()
// at random intervals add a random number to the collection.
// Every third iteration, the thread should remove two random
// elements from the collection.
int randomVal;
int randomListVal;
// int stepCounter = 0;
while (true)
{
lock (locker)
{
randomVal = random.Next(0, 20);
numList.Add(randomVal);
stepCounter += 1;
if (stepCounter % 3 == 0)
{
for (int i = 0; i == 2; i++)
{
randomListVal = random.Next(0, numList.Count);
numList.RemoveAt(randomListVal);
}
}
Thread.Sleep(randomVal * 100);
}
}
}
private void ThreadTask3()
{
//this the thread that will read and show data from the array created in ThreadTask2()
// at random intervals add a random number to the collection.
// Every third iteration, the thread should remove two random
// elements from the collection.
int randomVal;
int randomListVal;
while (true)
{
lock (locker)
{
randomVal = random.Next(0, 20);
numList.Add(randomVal);
stepCounter += 1;
if (stepCounter % 3 == 0)
{
for (int i = 0; i == 2; i++)
{
randomListVal = random.Next(0, numList.Count);
numList.RemoveAt(randomListVal);
}
}
Thread.Sleep(randomVal * 100);
}
}
}
private void ThreadTask4()
{
int[] array;
while (true)
{
lock (locker)
{
array = numList.ToArray();
for (int i = 0; i < array.Length; i++)
{
textBox1.Text += (array[i] + " ");
}
Thread.Sleep(1000);
textBox1.Text += ("\r\n");
}
}
}
private void Form1_Load(object sender, EventArgs e)
{
//creates a new instance of thread1
Thread trd = new Thread(new ThreadStart(this.ThreadTask));
//makes the new thread a background thread, allowing easy termination
//upon closing of the application
trd.IsBackground = true;
//starts the thread
trd.Start();
/*Same process as above, but for thread2*/
Thread trd2 = new Thread(new ThreadStart(this.ThreadTask2));
trd2.IsBackground = true;
trd2.Start();
Thread trd3 = new Thread(new ThreadStart(this.ThreadTask3));
trd3.IsBackground = true;
trd3.Start();
Thread trd4 = new Thread(new ThreadStart(this.ThreadTask4));
trd4.IsBackground = true;
trd4.Start();
}
}
}
}
有什么我想念的吗?其他一切似乎都做我想要的事情
答案 0 :(得分:1)
for
循环中的条件错误。
您应该检查i <= 2
而不是i == 2