P
P
Pavel Dudenkov2015-11-11 20:31:25
Java
Pavel Dudenkov, 2015-11-11 20:31:25

Multithreading not terminating?

This code freezes, if you change the value of totalCountSpeeches to 2000 - it ends.
I have a few assumptions, but they are just guesses, as there is not enough knowledge of how the JVM works with multiple threads...

public class Solution {
    public static int totalCountSpeeches = 200;
    public static int soundsInOneSpeech = 1000000;

    public static void main(String[] args) throws InterruptedException {
        Politic ivanov = new Politic("Иванов");
        Politic petrov = new Politic("Петров");
        Politic sidorov = new Politic("Сидоров");

        while (ivanov.getCountSpeaches() + petrov.getCountSpeaches() + sidorov.getCountSpeaches() < totalCountSpeeches) {
        }

        System.out.println(ivanov);
        System.out.println(petrov);
        System.out.println(sidorov);
    }

    public static class Politic extends Thread {
        private int countSounds;

        public Politic(String name) {
            super(name);
            start();
        }

        public void run() {
            while (countSounds < totalCountSpeeches * soundsInOneSpeech) {
                countSounds++;
            }
        }

        public int getCountSpeaches() {
            return countSounds / soundsInOneSpeech;
        }

        @Override
        public String toString() {
            return String.format("%s сказал речь %d раз", getName(), getCountSpeaches());
        }
    }
}

Answer the question

In order to leave comments, you need to log in

2 answer(s)
P
pbahushevich, 2015-11-11
@viperz

Runnable picture will not change.
First, comments on the code

while (ivanov.getCountSpeaches() + petrov.getCountSpeaches() + sidorov.getCountSpeaches() < totalCountSpeeches) {
        }

here, as I understand it, you are waiting for everyone to collect 200 votes together and after that the cycle will stop and you will have data on who got how many votes.
The problem is that the end of the loop occurs in the main thread, and the rest of the threads do not know about it and will continue to work. And between the end of the loop and the output of the results to the screen, 3 threads will have time to do a lot of things and the results will already be very different. Well, or not much))) i.e. the sum of exactly 200 will not converge.
The problem is that it hangs before you exit the loop, and this problem is solved in the line
by adding the magic word volatile.
why - see Java Memory Model or if you don't understand write in the comments ))

P
protven, 2015-11-11
@protven

Well, why do you think that you "hung"? Add debug printing, see if your code does something or not. There can be many problems, ranging from resource race and int overflow, for example.

while (countSounds < totalCountSpeeches * soundsInOneSpeech) {
                countSounds++;
                 System.out.println("name -> " + this.getName() + ", " + "count -> " + countSounds);
            }

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question