Avatar ·

What does the synchronized keyword mean in Java? Explain in simple terms with examples

Hello colleagues! I'm new to Java and I can't understand what the `synchronized` keyword does. I read the documentation, but it seems too complicated. I have a simple `Counter` class that increments a counter in multiple threads. I expect each thread to add 1000 to the total counter, but the result is always different: sometimes 1987, sometimes 2003, although it should be exactly 2000 (two threads of 1000 each). Here's my code: ```java public class Counter { private int count = 0; public void increment() { count++; } public int getCount() { return count; } } public class Main { public static void main(String[] args) throws InterruptedException { Counter counter = new Counter(); Thread t1 = new Thread(() -> { for (int i