As you can see I have already added the two lines(manager.enter(n) and manager.exit(n)) I’m not sure if its correctand I also need it to print 1 2 3 instead of how it prints from 3 21 now.
using System;
using System.Threading;
public class sequencedThread
{
public static void Main()
{
Manager manager = new Manager(3);
for (int i=1; i<=3; i++)
new NumberedThread(i, manager);
HW.print(“main completedn”);
Console.ReadKey();
}
}
class NumberedThread
{
int n;
Manager manager;
public NumberedThread(int n, Manager manager)
{ this.n = n;
this.manager = manager;
(new Thread(new ThreadStart(run))).Start();
}
public void run()
{
manager.enter(n);
HW.print(“[Start Thread ” + n + “]n”);
for (int i = 0; i < 5; i++)
HW.print(“{Working Thread ” + n + ” <” + i + “>}n”);
manager.exit(n);
HW.print(“[Completed Thread ” + n + “]n”);
}
}
class Manager
{
int next;
public Manager(int next)
{
this.next = next;
}
public void enter(int n)
{
HW.print(” (enter – Thread ” + n + ” next=” + next + “)n”);
lock(this) {
while (n != next) {
HW.print(” (enter – wait Thread ” + n + ” next=” + next +”)n”);
Monitor.Wait(this);
if ( n != next)
Monitor.Pulse(this);
}
}
}
public void exit(int n)
{
lock(this) {
HW.print(” (exit – Thread ” + n + ” next=” + next + “)n”);
while (n != next) {
HW.print(” (exit – wait Thread ” + n + ” next=” + next +”)n”);
Monitor.Wait(this);
if ( n != next)
Monitor.Pulse(this);
}
next–;
Monitor.Pulse(this);
}
}
}
class HW {
public static void print(string s) {
Console.Write(s); Console.Out.Flush();
Thread.Sleep(10);
}
}
You are given a program that can control the sequence in which threads execute rather than in any order as is the norm. Each thread is numbered 1, 2, or 3. By invoking a Manager class method enter(n), threads can be forced to wait to execute in their sequence turn. By invoking a Manager class method exit(n) a thread signals that it has completed and the next thread in sequence can execute. Add the two lines: i. manager.enter(n) i. manager.exit(n) to the run) method so that any thread can print Start but thread 3 always prints Working 0-4 and Completed before thread 2, thread 2 always prints Working 0-4 and Completed before thread 1. Modify the program in a minor way (i.e. two lines) to change the order in which the threads complete from 3, 2, 1 to 1, 2, 3. Show transcribed image text You are given a program that can control the sequence in which threads execute rather than in any order as is the norm. Each thread is numbered 1, 2, or 3. By invoking a Manager class method enter(n), threads can be forced to wait to execute in their sequence turn. By invoking a Manager class method exit(n) a thread signals that it has completed and the next thread in sequence can execute. Add the two lines: i. manager.enter(n) i. manager.exit(n) to the run) method so that any thread can print Start but thread 3 always prints Working 0-4 and Completed before thread 2, thread 2 always prints Working 0-4 and Completed before thread 1. Modify the program in a minor way (i.e. two lines) to change the order in which the threads complete from 3, 2, 1 to 1, 2, 3.
Expert Answer
Answer to As you can see I have already added the two lines (manager.enter(n) and manager.exit(n)) I’m not sure if its correct and…