class Program {
[MethodImpl(MethodImplOptions.Synchronized)]
public void m1() {
while (true) {
Console.WriteLine(“M1”);
}
}
[MethodImpl(MethodImplOptions.Synchronized)]
public void m2() {
while (true) {
Console.WriteLine(“M2”);
}
}
static void Main(string[] args) {
Program rapi = new Program();
MyThread1 t1 = new MyThread1();
MyThread1 t2 = new MyThread1();
t1.setR1(rapi);
t2.setR2(rapi);
Thread thread1 = new Thread(new ThreadStart(t1.run));
Thread thread2 = new Thread(new ThreadStart(t2.run));
thread1.Start();
thread2.Start();
}
}
class MyThread1 {
Program r1 = null;
Program r2 = null;
public void run() {
Console.WriteLine(“Thread is running…” + this);
if (r1 != null) {
r1.m1();
} else if (r2 != null) {
r2.m2();
}
}
public void setR1(Program r1) {
this.r1 = r1;
}
public void setR2(Program r2) {
this.r2 = r2;
}
}