Many methods with same name but different signature .
public static void Thread1() {
for (int i = 0; i < 10; i++) {
Console.WriteLine("Thread1 {0}", i);
}
}
public static void Thread2() {
for (int i = 0; i < 10; i++) {
Console.WriteLine("Thread2 {0}", i);
}
}
public class MyClass {
public static void Main() {
Console.WriteLine("Before start thread");
Thread t1=new thread(new threadstart(mythread.thread1))
Thread t2=new thread(new Threadstart(mythread.thread2));
This one have many methods(threadstart) with same name (threadstart) but different signatures ( mythread.thread1,mythread.start2)
Now both threads seem to execute in parallel. Here is a program which shows the usage of the other overloaded method of sleep.
public static void Thread1() {
for (int i = 0; i < 10; i++) {
Console.WriteLine("Thread1 {0}", i);
}
}
public static void Thread2() {
for (int i = 0; i < 10; i++) {
Console.WriteLine("Thread2 {0}", i);
}
}
public class MyClass {
public static void Main() {
Console.WriteLine("Before start thread");
Thread t1=new thread(new threadstart(mythread.thread1))
Thread t2=new thread(new Threadstart(mythread.thread2));
This one have many methods(threadstart) with same name (threadstart) but different signatures ( mythread.thread1,mythread.start2)
Now both threads seem to execute in parallel. Here is a program which shows the usage of the other overloaded method of sleep.
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(); } }Now the thread sleeps for one millisecond and gives the second thread an opportunity to execute. The output of this program is
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 9Now both threads seem to execute in parallel. Here is a program which shows the usage of the other overloaded method of sleep.
Comments
Post a Comment