Create a class, Counter, with a single private field variable of type int. You can call it count. It should be initialized to 0. Create
Create a class, Counter, with a single private field variable of type int. You can call it count. It should be initialized to 0.
Create a method, which when called will increment the int with 1.
Create a method to get the count variable.
Create a Runnable class, you can e.g. call it CountIncrementer, which takes a reference to your class with the counter. In the run() method, you call the update method 1.000.000 times, in a for loop. When the for-loop is done, get the count variable, and print it out.
Now create a Start class with a main method. Instantiate the Counter class, instantiate two instances of CountIncrementer, and start both Threads.
Two threads will now both increment the count variable 1.000.000 times. We would expect the printed result to be 2.000.000. Is that always so? Well discuss this next time.
Step by Step Solution
There are 3 Steps involved in it
Step: 1
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started