As far as I understand, the ReentrantLock class is an alternative to synchronized. And its tasks are to synchronize threads, if they work with a common object.
If something goes wrong, correct it.
public class TestMultithreading{ static int count = 0; // ΡΠΎΠ²ΠΌΠ΅ΡΡΠ½ΡΠΉ ΠΏΡΠΈΠΌΠΈΡΠΈΠ² Π½Π° 2 ΠΏΠΎΡΠΎΠΊΠ° public static void main(String[] args){ Thread t1 = new Thread(new Client()); Thread t2 = new Thread(new Client()); t1.start(); t2.start(); } } public class Client implements Runnable{ Lock lock = new ReentrantLock(); @Override public void run() { while (TestMultithreading.count <= 10) { lock.lock(); System.out.println(Thread.currentThread().getName() + " " + TestMultithreading.count); TestMultithreading.count++; lock.unlock(); } } Right:
Thread-0 0 Thread-0 1 Thread-0 2 Thread-0 3 Thread-0 4 Thread-0 5 Thread-0 6 Thread-0 7 Thread-0 8 Thread-0 9 Thread-0 10 Thread-1 0 Wrong:
Thread-0 0 Thread-0 1 Thread-0 2 Thread-1 0 Thread-1 4 Thread-1 5 Thread-0 3 Thread-1 7 Thread-1 8 Thread-1 9 Thread-1 10 Thread-0 8 The result is correct once every other time. Why is that?