Im not sure wht does the question want by meaning this “Each thread adds 1 to a variable sum that initially zero”…
First off; the question explicitly mentions "launching" threads which is bit different from creating a "pool" of threads. No need to extend the `Thread` class, just make your work class implement Runnable. Your logic is flawed in the sense that you are "not" sharing the MutableInteger between all those "work" instances. Each Runnable has its own "sum" variable and hence you don't see the effect of synchronization/non-synchronization since there is nothing shared.
You would have to:
- create two "runnable" classes (classes which implement Runnable interface); in one you will call `sum.inc()` and in the other class you will call `sum.syncInc()`. Let's call them Work and WorkSync respectively.
- These classes should have a constructor which takes a "MutableInteger" parameter.
- You create a single "Mutable" integer and pass it to all the 100 instances of your Work class instance. After that, create a new Thread for each Runnable and launch that thread (call start on it).
- Repeat the same for WorkSync class (make sure you start from scratch; i.e. create new Mutable integer, create new 100 threads etc.)
- Observe the result and draw your conclusion