我的线程有问题,这是一个代码:
class myThread
{
public int _start, _finish;
string[] new_array = new string[10];
public static string[] existed_array = new string[20];
public myThread(string name, int start, int finish)
{
_start = start;
_finish = finish;
Thread thread = new Thread(this.Get);
thread.Name = name;
thread.Start();//передача параметра в поток
}
void Get()
{
for (int ii = _start; ii < _finish; ii++)
{
// i put data in existed array in Main()
// new array is just an array where i want to put existed data
new_array[ii] = existed_array[ii];
// but in output new_array[0]=null; new_array[1]=value
}
}
}
void Main ()
{
// For example
myThread.existed_array = {1, 2 , 3, ...}
myThread t1 = new myThread("Thread 1", 0, 1);
myThread t2 = new myThread("Thread 2", 1, 2);
}
线程使用不同的参数运行 Get(),但在输出中只有第二个线程的参数。正如我从逐步程序中看到的那样,Get 函数中的每一行都运行了 2 次,所以这就是重点,我该如何解决这个问题?