The problem is that 2 or 3, or 10 threads (it does not matter), alternately (each thread alternately after one second), decrement the variable. At first, I simply wrote a method and synchronized it, then I synchronized the object, now I do everything directly in the run method, but still all threads work in a pack, and only after performing one iteration, back again, they all fall asleep in a pack. How to consistently divide the work? Why does not synchronization help?
package nhg; import java.net.UnknownHostException; public class qq { public static void main ( String [] args ) throws UnknownHostException, InterruptedException { for (int i = 0; i < 2; i++) { new The().start(); } } public static class The extends Thread { private volatile static int q = 10; private final Object lock; The(){ lock = new Object(); } @Override public void run() { synchronized (lock) { for (q = 10; q > 0; q--) { try { Thread.sleep(1000); } catch (InterruptedException e) { System.out.println("qqq"); } System.out.println(currentThread().getName() + " lift vniz " + q); } Thread.yield(); } } } }