在这个多线程程序中,我试图在填充它的每第三次迭代中从列表中删除两个随机元素。我想我可以通过计算“stepCounter”的模来跟踪有多少次新元素被添加到列表中(所有线程都执行相同的功能)并且在检查了几个断点之后,我发现它正在检查 stepCounter 条件,然后转到“for”循环的第一行以选择两个要删除的随机索引。
问题是实际上并没有执行删除,就好像它读取“for i = etc.”然后只是跳过。
namespace ThreadWinApp { public partial class Form1 : Form { // 这里为整个类创建线程变量 private Thread trd; 私有线程 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();
}
}
}
}
有什么我想念的吗?其他一切似乎都在做我想做的事