Question: 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

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!