0

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()); } } } 
3
  • Read numWaitingCustomer before you start your threads... store this value somwere and lock the access to it. In your implementation every thread reads the textBox1 content and has therefore its own copy of the "numWaitingCustomer" Commented Jul 24, 2015 at 10:27
  • Typical producer-consumer scenario, have a look at menu. Commented Jul 24, 2015 at 10:30
  • Using Threads and Sleep() is probably not the best way to do a simulation. Commented Jul 24, 2015 at 11:07

1 Answer 1

1

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) { ... } 
Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.