softwareconstruction240 / softwareconstruction

Content for BYU CS 240: Advanced Software Construction
MIT License
45 stars 39 forks source link

update(concurrency.md) Fully Update Concurrency Example #140

Closed frozenfrank closed 1 month ago

frozenfrank commented 1 month ago

Resolves #110.

Before submitting this PR, I recreated the sample to personally experience the effects of the code. This helped to provided a more accurate and clear description of the actual problem in the text. In testing, I extended the code with a few simple endpoints to fully support the operations needed.

Full Testing Code

public class MultithreadedServerExample {
    static int sum = 0;

    // Main function and other boilerplate statements omitted for clarity.

    Spark.get("/add/:value", (req, res) -> {
        sum += Integer.parseInt(req.params(":value"));
        return " " + sum;
    });
    Spark.get("/set/:value", (req, res) -> {
        sum = Integer.parseInt(req.params(":value"));
        return " " + sum;
    });
    Spark.get("/get", (req, res) -> " " + sum);
}
curl localhost:8080/set/0
for i in {1..100}; do curl localhost:8080/add/1; print ""; done &
for i in {1..100}; do curl localhost:8080/add/-1; print ""; done &
wait
curl localhost:8080/get; print"";
frozenfrank commented 1 month ago

@leesjensen Here is a screenshot of a few test runs. Notice the final results are circled, and the commands are highlighted in yellow for clarity.

Screenshot 2024-09-24 at 8 18 44 AM