Click here to Skip to main content
15,562,816 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I am using following example .

using System;
using System.Threading;

public class MyThread {

        public void Thread1() {
                for (int i = 0; i < 10; i++) {
                        Console.WriteLine("Hello world " + i);
                        Thread.Sleep(1);
                }
        }
}

public class MyClass {

        public static void Main() {
                Console.WriteLine("Before start thread");

                MyThread thr1 = new MyThread();
                MyThread thr2 = new MyThread();

                Thread tid1 = new Thread(new ThreadStart(thr1.Thread1) );
                Thread tid2 = new Thread(new ThreadStart(thr2.Thread1) );

                tid1.Start();
                tid2.Start();
        }
}


It works like this.

output :
Before start thread
Hello world 0
Hello world 0
Hello world 1
Hello world 1
Hello world 2
Hello world 2
Hello world 3
Hello world 3
Hello world 4
Hello world 4
Hello world 5
Hello world 5
Hello world 6
Hello world 6
Hello world 7
Hello world 7
Hello world 8
Hello world 8
Hello world 9
Hello world 9


It print i from first for loop and sleep for 1 sec move to second for loop.It prints i from second for loop sleep for 1 sec and move to first.I don't want it to context switch after every i print.I want like this after certain time after printing some value of i,sleep for some time within that period print value of i from second loop.After that expire again move back to first loop.Ex: it suppose to print from 1 to 2-10 from first for loop and sleep move to second,it may print 1to 2-10 from second for loop.and move to first loop.Value of i from from first loop may not be same for second.

What I have tried:

using System;
using System.Threading;

public class MyThread {

        public void Thread1() {
                for (int i = 0; i < 10; i++) {
                        Console.WriteLine("Hello world " + i);
                        Thread.Sleep(1);
                }
        }
}

public class MyClass {

        public static void Main() {
                Console.WriteLine("Before start thread");

                MyThread thr1 = new MyThread();
                MyThread thr2 = new MyThread();

                Thread tid1 = new Thread(new ThreadStart(thr1.Thread1) );
                Thread tid2 = new Thread(new ThreadStart(thr2.Thread1) );

                tid1.Start();
                tid2.Start();
        }
}
Posted
Updated 27-May-18 22:51pm
Comments
Richard MacCutchan 28-May-18 3:47am    
Change the calls to Sleep to use random values.

1 solution

First off, Sleep(1) is not "sleep for one second" - it's "sleep for one millisecond" - a thousandth of a second.

Secondly, the two thread execute the same code, but they don't share variables:
for (int i = 0; i < 10; i++)
creates a new instance of i in each thread, so they will have different values and will not affect each other.
You cannot guarantee which thread will run at any specific time - that is totally up to the operating system, which will pick threads which aren't waiting for something (like the Sleep timer) effectively at random as soon as a core becomes available - which means they can actually ruin at the same time if you have sufficient cores and the system isn't too busy: if you want to control what happens when in a multithreaded system, you are responsible for explicitly make it happen, most often with semaphores.

If you want thread two to wait for thread one, you need to make it wait until thread one is ready for it - and that means they can't share the same code, or thread 1 will end up waiting for itself to finish!

And to make it worse ... the main method is not being executed on either of those threads, so when it gets to the end your app will close and you won't see anything...

I suggest you go back to your course notes and read them again - this is a complicated subject and you don't seem to have grasped it properly yet.
 
Share this answer
 

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