0

I wrote this code as a Runnable in Java, but my professor wants me to add AtomicInteger so that there is no thread interference. How do I go about doing that? I have tried looking up examples of how to use it in code but I have no idea what to do in this case.

public class GarageWorker {
    public static void main(String[] args) {
        System.out.println("We are going to count the vehicles in the garage");
        System.out.println();
        System.out.println("There are 50 vehicles in the garage to start with!");
        System.out.println();

        GarageWorker garage = new GarageWorker(50);

        Runnable vehicleEnter = garage.new Enter();
        Runnable vehicleExit = garage.new Exit();

        Thread thread1 = new Thread(vehicleEnter);
        Thread thread2 = new Thread(vehicleExit);

        thread1.start();
        thread2.start();
    }

    public GarageWorker(int initialCarCount) {
        counter = initialCarCount;
    }

    public int counter;

    public class Enter implements Runnable {
        public Enter() {

        }

        public int increaseVehicleCount() {
            return ++counter;
        }

        public void run() {
            while (counter < 100) {
                System.out.println("One vehicle entered the garage now there are " + increaseVehicleCount());
            }
        }
    }

    public class Exit implements Runnable {
        public Exit() {

        }

        public int decreaseVehicleCount() {
            return --counter;
        }

        public void run() {
            while (counter > 0) {
                System.out.println("One vehicle left the garage now there are " + decreaseVehicleCount());
            }
        }
    }
}

The code runs fine, but my professor wants me to implement the AtomicInteger class into the code.

Mr. Polywhirl
  • 42,981
  • 12
  • 84
  • 132
Zane Pace
  • 3
  • 4
  • So, you want to instantiate your `GarageWorker` and make the `counter` instance variable of type `AtomicInteger`. Can you provide an example of how you want to go about this at the bottom of your post? – Mr. Polywhirl Oct 14 '19 at 22:54
  • 1
    Yes so that instead of using the garage counter I replace it with the AtomicInteger. – Zane Pace Oct 14 '19 at 22:56

1 Answers1

2

I assume that your professor wants you to change counter to an AtomicInteger. First you want to import AtomicInteger as well as change the declaration of counter and the constructor to reflect that.

import java.util.concurrent.atomic.AtomicInteger;
public GarageWorker(int initialCarCount) {
    counter = new AtomicInteger(initialCarCount);
}

public AtomicInteger counter;

Also, you want to change the increaseVehicleCount and decreaseVehicleCount methods in order to use AtomicInteger instead of int.

public int increaseVehicleCount() {
    return counter.incrementAndGet();
}
public int decreaseVehicleCount() {
    return counter.decrementAndGet();
}

incrementAndGet as well as decrementAndGet do just as they say: they increment (or decrement) and subsequently returns the new value. For more information, take a look at the Javadoc.

Cheers!

Leftist Tachyon
  • 304
  • 1
  • 15