0

In the course of training, as well as with the analysis of multithreading, I decided to try my hand at thread synchronization. In this regard, I made a small test, where the creation of threads occurs through the creation of objects. And I got into a problem situation, the streams cannot be synchronized, and I don't understand what the problem is.

public class MainClass {
    static int firstInt;
    static int secondInt;
    static int thirdInt;
    public static void main(String[] args) {
        SecondClass s1 = new SecondClass("Первый");
        SecondClass s2 = new SecondClass("Второй");
        SecondClass s3 = new SecondClass("Третий");
        SecondClass s4 = new SecondClass("Четвертый");
        SecondClass s5 = new SecondClass("Пятый");

        try {
            s1.t.join();
            s2.t.join();
            s3.t.join();
            s4.t.join();
            s5.t.join();
        } catch (InterruptedException exp) {
            System.out.println("Прервано!");
        }

        System.out.println(firstInt);
        System.out.println(secondInt);
        System.out.println(thirdInt);
    }
}

class SecondClass implements Runnable {
    String name;
    Thread t;

    SecondClass(String a) {
        name = a;
        t = new Thread(this);
        t.setName(name);
        t.start();
        System.out.println("Поток с именем "+t.getName()+" создан и запущен.");
    }

    private void plusFirstInt() {
        MainClass.firstInt++;
    }

    private void plusSecondInt() {
        MainClass.secondInt++;
    }

    private void plusThirdInt() {
        MainClass.thirdInt++;
    }

    private synchronized void fori() {
        try {
            for (int i = 0; i < 10000; i++) {
                plusFirstInt();
                plusSecondInt();
                plusThirdInt();
            }
            Thread.sleep(10);
        } catch (InterruptedException exp) {
            System.out.println("Прервано!");
        }
    }

    @Override
    public void run() {
        fori();
    }
}

Tried to sync using object reference.

Object a = new Object();

private void plusFirstInt() {
    synchronized (a){
        MainClass.firstInt++;
    }
}
John Kugelman
  • 349,597
  • 67
  • 533
  • 578
Fobos1996
  • 11
  • 1
  • 4
    You have five different instances in five different threads, and each one is synchronized on it's own instance - they use different synchronization so they won't block each other. – Mureinik Dec 20 '22 at 19:54
  • What exactly do you mean by "he streams cannot be synchronized"? – Progman Dec 20 '22 at 19:55
  • 1
    Also note: when writing questions about "not working code", please follow [mcve]. Don't expect us to figure your problem from "not working, here my code". Always tell us what you expect to happen, and what actually happens. "doesnt work" isnt a working problem description. – GhostCat Dec 20 '22 at 19:56
  • I expect the output to be like this: 50000 50000 50000 but the output is: 48359 48938 48429 – Fobos1996 Dec 20 '22 at 20:01
  • `synchronized (SecondClass.class)` should work for example. The synchronization object needs to be shared across all instances of `SecondClass`, i.e. it has to be `static` or something that is inherently static like a class object – zapl Dec 20 '22 at 20:06
  • Thank you all for your help, I figured out my issue with your help. – Fobos1996 Dec 22 '22 at 17:45

0 Answers0