![]() | |
Teachers Assignments Other Pages | Project 0: Assigned: Fri Jan 12 2024 In this project, you will implement a Thread-safe semaphore that can handle multiple tokens. Remember: you are not allowed to use the built-in Semaphore class in your implementation. Although you are welcome to add any private methods you'd like, please be wary of adding extra public methods, as they may make your Semaphore able to reach inconsistent states. Part 0, 0 points: Create a new Java class, Part 1, 0 points: Add the class header to your file: Part 2, 0 points: Add an integer field to your semaphore. I named mine numTokens: Part 3, 0 points: Add a constructor that takes an integer representing the number of tokens available when it's created. Users of your code should be able to create a semaphore like this: Part 4, 0 points: I'm not going to grade it, but I highly recommend you make a toString method, as you should when making a new Java class. Part 5, 0 points: You will also need a getNumTokens() method that returns the number of available tokens in the semaphore. Part 6, 0 points: Now it's time to write the two main methods: p() and v(). I'll give you a bad version of p() which you can update later. Note: p is short for the Dutch word proberen. This method blocks until the number of tokens the semaphore has becomes positive. (If there are already positive tokens, then it won't block at all.) When it's done blocking, it removes a token and then is done. Here's a very bad way to implement this that unnecessarily waits and is not thread safe: Part 7, 0 points: Add a new method, Part 8, 0 points: You should now be able to test your code with the Part 9, 0 points: Here's one possible sequence of steps that could cause a race condition. There is a semaphore with 1 token and two threads are simultaneously calling the Part 10, 10 points: You need to modify your code so that it is thread-safe; i.e. no race conditions can occur. In order to do this, you can use two Java synchronization tools: Part 11, 10 points: In addition to making sure your code is correct, you also want to improve performance (speed). Make sure that there are no calls to Part 12, 20 points: Ensure that your semaphore doesn't block on calls to Part 13, 20 points: Make sure that your semaphore blocks in Part 14, 20 points: There is no efficient test that will always be able to find all the race conditions. (Detecting deadlocks is PSPACE-hard.) Nevertheless, I wrote a stress test to try to kill your Semaphore in my testing code. If your code passes those final stress tests (with whatever parameters I choose) then you will earn the points for this. Part 15, 20 points: No possible race conditions or deadlocks! I am going to look at your code and see if I can find a potential race condition or deadlock. If I find one or if your code doesn't pass the previous part, then you won't earn points for this part. I am willing to look at your code beforehand and see if I can find problems, but: Part 16, 0 points: If your code runs fast enough when I test it, you'll earn up to 5 bonus points. If you submit your code, you can ask me to run it to see how fast it runs. I'm definitely willing to chat about potential ways to speed up your code! Submitting your Project: After you've succeeded all the tests and are pretty sure you don't have any Race Conditions, upload your MySemaphore.java file to the Canvas assignment. If you've submitted well before the deadline, send me a message and ask me to run the test myself. Remember that I need to be able to run your code via the command line. (I don't know how to use your IDE.) I will download it to a directory with the SemaphoreTester and then type these commands: $ javac *.java
$ java -ea SemaphoreTester 15000 If I can't compile and run it like that, I will ask you to fix the code and resubmit and may be subject to lateness penalties. If your code runs on my machine (and there is still time before the deadline) you can ask me to look ahead of time for race conditions. Thus, submit early and often! |