‘Threading in the Gym’ project in ‘Learn Intermediate Java’ course - ADDITIONAL CHALLENGES (task 32)

The project itself:
https://www.codecademy.com/courses/learn-intermediate-java/projects/threading-in-the-gym

Can anyone describe the logic for how to deal with the additional challenge in the ‘Threading in the Gym’ project?
Should I use synchronized(), wait(), and notifyAll() (plus necessary changes to machineType/weigth-InUse states) as many times as there are different machine types and weights? Is the openForTheDay() method in Gym.java the right place to include all those limitations and dependency?

And one other question.
I’ve tried to follow all instructions quite exactly, but there is something wrong (?) with the output I get when gym members finish they work. It looks ok up to the moment when only 4 out of 5 are still exercising. The staff informs me about members that theoretically are done (these threads are no longer alive), but… they are still working like the Member1 in example given below. Any idea why?


Gym Staff - 2 people are working currently: [Thread-0, Thread-4]

Gym Member 1 performing exercise: Lat Pull Downs with 110 lbs of total weight consisting of 3 x 5 lbs weights, 2 x 10 lbs weights, 3 x 25 lbs weights.

Is it like that because the numbering of threads is different from the numbering of gym members (their id-s)?

Thanks in advance!!

It looks like Codecademy didn’t keep my code saved for this project, so I can’t remember enough to answer specifically, but the project that’s about baking a cake (or something) basically walks you through how to do the same thing. So you could always go do that one and come back to the gym one. Just a thought.

I am looking for the answer to first part as well. I am confused where I should use synchronised(this) in the code. If anyone can find the answer, it will be appreciated.

I’d look at the Cakemaker.java program and adapt it to the project. The sync code should be where you return the new thread. Use data from availableMachines and choose when to sync or not

I know this is very late, but basically all you need to do is to make incrementer and decrementer methods in both Gym and Weight, and a method that checks if the weights in the gym are less than the weights required for the particular exercise, and a method to check if the number of available machines of a certain machinetype is 0, in (synchronized(this) block) in a while loop check if one of these methods is true, if yes that means the user can’t do the exercise, so you print a message that the member is waiting and then wait(), after exiting print that the member is now exercising and call the decrementer methods for Gym and Weight, then after the thread sleep command, again in a synchronized block, call the incrementer methods then print the member finished the exercise, make sure that you either make the availableMachines field of Gym is public static or private but modify the Member constructor to include a referance to the gym,(i used the second way) also dont forget to add ALL the machineType values to the availableMachines map because the code snippet they gave you doesn’t include all MachineType values which will lead to nullpointerexceptions popping up.

1 Like

Awesome explanation, @bakrmohamedbakr08068
Thank you!

However, I couldn’t get it to work until I notice that you create the threads in class Gym and synchronized them in class Member, so I pointed sentence with the object referenced in synchronized block (not “this” because Member doesn’t contain its own threads), such as:

Gym gym;

synchronized(gym) {
while (conditionHaveToWait) {

gym.wait();
}
Other tasks if thread doesn’t have to wait
}

Other tasks…

synchronized(gym) {
Update vars that control wait event

gym.notifyAll();
}