问题是:我已经说过每个线程应该按顺序使用的 5 种方法
方法1();然后方法2();然后 Method3(); 然后 Method4(); 然后 Method5();
我还运行了 5 个线程,编号从 1 到 5
我想实现以下场景:
我希望线程 1 开始使用 method1 然后移动到 method2 [并行我希望线程 2 开始使用现在未使用的 method1]
然后当线程 1 移动到方法 3,线程 2 继续到方法 2 时,线程 3 应该开始使用现在空闲的方法 1,依此类推。
public void Execute(object OPCounter)
{
//Method 1
lock (thisLock)
{
FetchedInstructionQueue[PCounter] = Stager.Stage1(InstructionsMemory);
}
//Method 2
lock (thisLock)
{
DecordedInstructionQueue[PCounter] = Stager.Stage2(FetchedInstructionQueue, regMem);
}
//Method 3
lock (thisLock)
{
ALUResultQueue[PCounter] = Stager.Stage3(DecordedInstructionQueue);
}
lock (thisLock)
{
MemoryQueue[PCounter] = Stager.Stage4(DecordedInstructionQueue, memory, ALUResultQueue);
}
lock (thisLock)
{
object obj = Stager.Stage5(DecordedInstructionQueue, ALUResultQueue, regMem, memory, MemoryQueue);
InternalWriter(PCounter, obj);
}
}
///This is the initiator of threads
private void ExecuteBtn_Click(object sender, EventArgs e)
{
InstructionsMemory = InstructionsTextBox.Text.Split('\n');
FetchedInstructionQueue = new string[InstructionsMemory.Length];
DecordedInstructionQueue = new Instruction[InstructionsMemory.Length];
ALUResultQueue = new int[InstructionsMemory.Length];
MemoryQueue = new int[InstructionsMemory.Length];
Thread[] threads = new Thread[InstructionsMemory.Length];
for (APCounter = 0; APCounter < InstructionsMemory.Length; APCounter = 5 + APCounter)
{
if (APCounter + 5 < InstructionsMemory.Length)
{
object s1 = APCounter;
object s2 = APCounter + 1;
object s3 = APCounter + 2;
object s4 = APCounter + 3;
object s5 = APCounter + 4;
threads[APCounter] = new Thread(new ParameterizedThreadStart(Execute));
threads[APCounter + 1] = new Thread(new ParameterizedThreadStart(Execute));
threads[APCounter + 2] = new Thread(new ParameterizedThreadStart(Execute));
threads[APCounter + 3] = new Thread(new ParameterizedThreadStart(Execute));
threads[APCounter + 4] = new Thread(new ParameterizedThreadStart(Execute));
threads[APCounter].Start(s1);
threads[APCounter + 1].Start(s2);
threads[APCounter + 2].Start(s3);
threads[APCounter + 3].Start(s4);
threads[APCounter + 4].Start(s5);
}
}