使用线程的FIFO进程调度

时间:2013-03-04 04:27:07

标签: c# multithreading

我正在尝试使用线程创建先进先出流程调度。在每个线程完成之后,我希望它暂停它或将其移动到等待队列中,以获得10到20之间的随机数量的滴答,然后让它重新跳回。我希望整个模拟运行到250个滴答,然后终止但是我在第一个“线程正在运行或终止:它无法重启”之后得到此错误。不知道从哪里开始,有什么想法吗?我的代码如下。

public class Alpha
{
    int counter = 0;
    private int sleepTime;
    private static Random rand = new Random();

    public Alpha()
    {
        sleepTime = rand.Next(10001);
    }

    public void Process1()
    {
            Console.WriteLine(string.Format(
                "Process 1 RUNNING (run#: {0})", counter));
            Console.WriteLine();


            Thread current = Thread.CurrentThread;
            Console.WriteLine("{0} going to sleep", current.Name, sleepTime);
            Console.WriteLine();
            Thread.Sleep(sleepTime);
            Console.WriteLine("{0} done sleeping", current.Name);
            Console.WriteLine();    
    }

    public void Process2()
    {
            Console.WriteLine();
            Console.WriteLine(string.Format(
                "Process 2 RUNNING (run#: {0})", counter));
            Console.WriteLine();

            Thread current = Thread.CurrentThread;
            Console.WriteLine("{0} going to sleep", current.Name, sleepTime);
            Console.WriteLine();
            Thread.Sleep(sleepTime);
            Console.WriteLine("{0} done sleeping", current.Name);
            Console.WriteLine();
    }

    public void Process3()
    {
          Console.WriteLine(string.Format(
              "Process 3 RUNNING (run#: {0})",counter));
          Console.WriteLine();

          Thread current = Thread.CurrentThread;
          Console.WriteLine("{0} going to sleep", current.Name, sleepTime);
          Console.WriteLine();
          Thread.Sleep(sleepTime);
          Console.WriteLine("{0} done sleeping", current.Name);
    }

    public void Process4()
    {
          Console.WriteLine(string.Format(
              "Process 4 RUNNING (run#: {0})", x));
          Console.WriteLine();

          Thread current = Thread.CurrentThread;
          Console.WriteLine("{0} going to sleep", current.Name, sleepTime);
          Console.WriteLine();
          Thread.Sleep(sleepTime);
          Console.WriteLine();
          Console.WriteLine("{0} done sleeping", current.Name);
          Console.WriteLine();  
    }
};

public class StepOne
{
    public static void Main()
    {
        Alpha oAlpha = new Alpha();

        Thread bThread = new Thread(new ThreadStart(oAlpha.Process1));
        bThread.Name = "Process 1";
        Thread cThread = new Thread(new ThreadStart(oAlpha.Process2));
        cThread.Name = "Process 2";
        Thread dThread = new Thread(new ThreadStart(oAlpha.Process3));
        dThread.Name = "Process 3";
        Thread eThread = new Thread(new ThreadStart(oAlpha.Process4));
        eThread.Name = "Process 4";

        for (int a = 0; a <= 250; a++)
        {
            bThread.Start();
            while (!bThread.IsAlive) ;
            bThread.Join();
            Console.WriteLine();

            cThread.Start();
            while (!cThread.IsAlive) ;
            cThread.Join();
            Console.WriteLine();

            dThread.Start();
            while (!dThread.IsAlive) ;
            dThread.Join();
            Console.WriteLine();

            eThread.Start();
            while (!eThread.IsAlive) ;
            eThread.Join();
            Console.WriteLine();
        }
    }
}

}

1 个答案:

答案 0 :(得分:2)

当线程proc完成时,它终止线程。你必须在线程方法中放置一个循环以使其保持活动状态 - 你可以让它阻塞同步原语,如事件,然后在你希望该线程恢复工作时从其他地方发出该事件的信号。然后,线程可以从队列中获取其工作项。这基本上是内置线程池只能以比我们其他人希望实现的更好的方式。你有什么理由不能只使用内置的线程池吗?