Answer to Question #167759 in Python for Usama Amjad

Question #167759

Coding

The minimal requirements of your program are:

•                 Present the user with a menu, wherein s/he may decide:

◦  Which dishes to make (with up to four dishes, this means up to 4 threads)

◦  The number of each dish to prepare (ideally a separate number for each dish)

◦  How often should progress be reported? (Explained further below)

•                 Start a thread for each dish

◦  Each type should be implemented into its own thread, since it has its own lock requirements

◦  Each dish tries to report upon completing one (subject to above/below)

◦  Each dish type always reports when completed its iterations

▪  Obviously there's no reporting if none are being made at all

•                 The program then reports on the total number of dishes produced

•                 If this isn't obvious, increasing the tally and output are also a 'resource'



Additional notes:

Write your threads immensely clearly, and comment it.

•                 Comment usefully. Don't add nonsense like identifying that a loop “loops”, or that .lock() “locks”

•                 Also, proper indentation, please?

Obviously, you'll have several .java files for this (if using Java).

Pseudo Code:

import threading


todo=2000000

confirmed=0

lock=threading.Semaphore() #We had some interesting options here


def worker(id):

global todo, confirmed

print("\t"+str(id)+" thinking...")

while True:

#We could do extra work here


lock.acquire() #Either acquire

the lock, or block until it's ready

if todo==0:

break

todo-=1

confirmed+=1

lock.release()  

#We could also do work here

print("\t"+str(id)+" completed!")


t1=threading.Thread(target=worker, args=(1,))

t2=threading.Thread(target=worker, args=(2,))

t1.start()

t2.start()

t1.join()

t2.join()

print("Done!\n"+str(confirmed))

Output Should be like

would you like any frozen pizzas? (y/n)y

How many would you like 1000000

would you like any pastas? (y/n)y

How many would you like 1000000

would you like any pizzas? (y/n)y

How many would you like 1000000

would you like any chicken? (y/n)y

How many would you like 1000000

At what rate would you like to recieve reports? 100000

Frozen pizzas starting...

Pastas starting...

Pizzas starting...

Chicken starting...

100000 Frozen Pizzas completed!

200000 Frozen Pizzas completed!

300000 Frozen Pizzas

completed!

400000 Frozen Pizzas completed!

500000 Frozen Pizzas completed!

600000 Frozen Pizzas completed!

700000 Frozen Pizzas completed!

100000 Chickens completed!

800000 Frozen Pizzas completed!

900000 Frozen Pizzas completed!

1000000 Frozen Pizzas completed!

Frozen Pizzas Complete

100000 Pastas completed!

100000 Pizzas completed!

200000 Chickens completed!

200000 Pizzas completed!

300000 Chickens completed!

300000 Pizzas completed!

200000 Pastas completed!

400000 Chickens completed!

400000 Pizzas completed!

500000 Chickens completed!

300000 Pastas completed!

500000 Pizzas completed!

600000 Chickens completed!

600000 Pizzas completed!

700000 Chickens completed!

400000 Pastas completed!

700000 Pizzas completed!

800000 Chickens completed!

800000 Pizzas completed!

500000 Pastas completed!

900000 Chickens completed!

900000 Pizzas completed!

1000000 Chickens completed!

Chicken Complete

600000 Pastas completed!

1000000 Pizzas completed!

Pizzas Complete

700000 Pastas completed!

800000 Pastas completed!

900000 Pastas completed!

1000000 Pastas completed!

Pastas Complete

Done!

4000000 items made



1
Expert's answer
2021-03-01T16:02:58-0500
import threading

todo = 2000000
confirmed = 0

lock = threading.Semaphore()

def worker(id):
    global todo, confirmed
    print("\t"+str(id)+" thinking...")

while True:
    lock.acquire()
    if todo==0:
        break
    else:
        todo -= 1
        confirmed += 1

lock.release()
print("\t"+str(id)+" completed!")

t1=threading.Thread(target=worker, args=(1,))
t2=threading.Thread(target=worker, args=(2,))
t1.start()
t2.start()
t1.join()
t2.join()

print("Done!\n"+str(confirmed))

Need a fast expert's response?

Submit order

and get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!

Comments

No comments. Be the first!

Leave a comment

LATEST TUTORIALS
New on Blog
APPROVED BY CLIENTS