Fix that Java: Increasing Value


Threads’ execution easily ends up in corrupted data.

class Main {
    public static void main(String[] args) throws InterruptedException {
        Counter counter = new Counter();
        Thread thread = new CounterThread(counter);
    }
}
class Counter {
    private volatile int value = 0;

    public void increase() {
        value += 1;
    }

    public int getValue() {
        return value;
    }
}
class CounterThread extends Thread {
    private final Counter counter;

    public CounterThread(Counter counter) {
        this.counter = counter;
    }

    @Override
    public void run() {
        for (int i = 0; i < 10000; i += 1) {
            counter.increase();
        }
    }
}

Fix that code!