Click here to Skip to main content
15,867,594 members
Please Sign up or sign in to vote.
4.00/5 (1 vote)
I am having some confusions regarding multithreading, and regarding how it runs.

This is my code.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;

namespace MultiThreading
{
    class Program
    {
        static void Main(string[] args)
        {
            Operations op = new Operations();
            Thread Th1 = new Thread(new ThreadStart(op.Op1));
            Thread Th2 = new Thread(new ThreadStart(op.Op2));
            //Thread Th3 = new Thread(new ThreadStart(op.Op3));
            Th1.Start();
            Th2.Start();
            //Th3.Start();
            Console.ReadLine();
        }
    }
    class Operations
    {
        public void Op1()
        {
            Console.WriteLine("Thread 1 operation A");
            Thread.Sleep(2000);
            Op3();
            Console.WriteLine("Thread 1 operation B");
            Thread.Sleep(2000);
            Op3();
            Console.WriteLine("Thread 1 operation C");
        }
        public void Op2()
        {
            Console.WriteLine("Thread 2 operation A");
            Thread.Sleep(2000);
            Console.WriteLine("Thread 2 operation B");
            Thread.Sleep(2000);
            Console.WriteLine("Thread 2 operation C");
        }
        public void Op3()
        {
            Console.WriteLine("Do Nothing");
        }
    }
}



The output I get is
Thread 1 operation A
Thread 2 operation A
Thread 2 operation B
Do Nothing
Thread 1 operation B
Do Nothing
Thread 1 operation C
Thread 2 operation C


However, the very next time I run, I get a varying outputs. Can anyone explain why this happens

EDIT:
If it is normal and it is the Scheduling Algorithm who decides which to run, is there any way I can control the threads to make the output appear in a format that I want ?
Posted
Updated 11-Apr-14 0:22am
v2
Comments
johannesnestler 11-Apr-14 7:50am    
I think you are not talking about "format" but order of you Output? Maybe have a look at Tasks and continuations http://msdn.microsoft.com/de-de/library/system.threading.tasks.task(v=vs.110).aspx In my opinion controlling threads directly isn't "state of the art" in .NET anymore - use of TPL http://msdn.microsoft.com/de-de/library/dd460717(v=vs.110).aspx is highly recommended. From your question it seems you are a "beginner" with multithreading concepts, maybe a little research could help?
Divakar Raj M 11-Apr-14 8:28am    
Thanks for the help :)

1 solution

it is normal.it is the Scheduling Algorithm who decides which to run.
 
Share this answer
 
Comments
Divakar Raj M 11-Apr-14 6:21am    
Is there any way I can control the threads and make the output appear in a specific format which I want ?

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900