Reputation: 35
I have 4 tasks to complete using multithreading concept. and I have 4 Waitress.
How to assign the waitress that are free to handle either one of the task??
-I kept on getting duplicated data (For eg : 4 Thread doing the similar task) -If i used lock, it locks 1 thread to do 4 tasks which is not wat i want
my code:
private void MTDemo()
{
String threadName = Thread.CurrentThread.Name;
while (true)
{
int numWaitingCustomer = 0;
Int32.TryParse(textBox1.Text, out numWaitingCustomer);
if (numWaitingCustomer > 0)
{
String takeOrderString = String.Format("Take order {0},{1}", threadName, numWaitingCustomer);
String serveMeal = String.Format("Serve meal {0},{1}",threadName, numWaitingCustomer);
String billCustomer = String.Format("Bill Customer {0},{1}", threadName, numWaitingCustomer);
String cleanTable = String.Format("Clean Table {0},{1}", threadName, numWaitingCustomer);
OutputMessage1(takeOrderString); Thread.Sleep(500);
OutputMessage1(serveMeal); Thread.Sleep(500);
OutputMessage1(billCustomer); Thread.Sleep(500);
OutputMessage1(cleanTable); Thread.Sleep(500);
numWaitingCustomer--;
SetControlPropertyValue(textBox1, "text", numWaitingCustomer.ToString());
}
}
}
Upvotes: 0
Views: 101
Reputation: 8986
Have each thread loop through the task list and complete the nth task, so waitress one will complete tasks 0,4,8... waitress 2 complete tasks 1,5,9... etc.
for(int i = threadNumber; i < taskList.length-threadNumber; i = i*totalThreads+ThreadNumber) {
...
}
Upvotes: 1