user2130381
user2130381

Reputation: 11

FIFO Process Scheduling Using Threads

I am trying to create a First in First Out Process Scheduling using threads. After each thread has completed, i want it to suspend it or move it to a wait queue for a random number of ticks between 10 and 20 and then have it jump back in. I want the whole simulation to run until 250 ticks and then terminate but i get this error just after the first one "Thread is Running or Terminated: It Cannot Restart". Don't really know where to go from here, any ideas? My code is below.

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();
        }
    }
}

}

Upvotes: 1

Views: 617

Answers (1)

When the thread proc completes it terminates the thread. You have to put a loop in the thread method to keep it alive - you would have it block on a synchronization primitive, like an event, and then signal that event from elsewhere when you want that thread to resume work. The thread could then get its work item from a queue. This is basically what the built-in thread pool does only in better ways than what the rest of us can hope to achieve. Any reason you can't just use the built-in thread pool?

Upvotes: 2

Related Questions