1

I would appreciate it if you could help me understand how one can approach multithreading and avoid starvation.

I have an array of the same class, for instance, class Animal, which should walk, eat, speak, etc. I have thought of using every Class Animal thread (By implementing Runnable). Still, I encountered the issue that I have to use Thread.sleep() in the while loop so that every other Animal in my array of Animals will have time to execute tasks.

My issue with the sleep approach is that I don't know how much time each thread should sleep, and it seems to be wrong to make the thread sleep when he can instead work (For example, he is the only thread running now).

My question is how can I avoid using Thread.sleep() and prevent Thread starvation when I have multiple threads of the same class.

The main loop where I check for changes in the Animals and repaint

@Override
public void run() {
    while (true) {
        repaint();
        try {
            sleep(500);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        checkIfEat(); // Iterate over all animals and see if they can eat eachother
    }
}

The thread the Animal object uses

@Override
public void run() {
    Point currentLocation;
    while (true) {
        currentLocation = getLocation();
        move(currentLocation.getX() + getHorSpeed() * getX_dir(), currentLocation.getY() + getVerSpeed() * getY_dir()); // move the animal to other location

    }
}
Gleb
  • 23
  • 4
  • 1
    `Thread.yield()`? – Johannes Kuhn May 18 '22 at 14:05
  • 2
    Can you please add more code with an example of your problem – ikorennoy May 18 '22 at 14:15
  • @JohannesKuhn Thank you very much; I think that's what I needed. @ikorennoy Regarding the code, I don't have much to show. The code itself is enormous, and the main issue is that I have an array of objects. Each of these objects has a thread running in a `while(true)` loop, So it doesn't have much to do with the code itself but more with the approach. – Gleb May 18 '22 at 14:26
  • 1
    `Thread.yeild()` is a very specific mechanism that needs to be used with great care, as you can see from the javadoc to this call. From your description, I assume that maybe you need to reconsider the work algorithm and look at the work stealing approach. – ikorennoy May 18 '22 at 14:37
  • Your computer only has so many CPU cycles available in any given period of time. If your program needs more, that's not starvation. Starvation is what we call it when the threads fail to share the available CPU cycles fairly. – Solomon Slow May 18 '22 at 15:42
  • If your program needs more CPU cycles and it already is using them effectively, then there's nothing else you can do except move your application to a bigger computer, or move it to a _cluster_ of computers. On the other hand, if you think your program is _wasting_ CPU cycles, then you will need to show us the code in order to get any help with that. What are these `//...commands that take time`? – Solomon Slow May 18 '22 at 15:43
  • @SolomonSlow My program doesn't need more CPU cycles. I'm talking about thread starvation mainly caused by bad design; getting a "bigger computer" is the wrong solution and is not related to the question asked. If you use `Thread.sleep()` on many threads, no matter how big the computer is, you will quickly get the OutOfMemmory error. And that's what I'm asking If there is another "More efficient" approach to this. – Gleb May 18 '22 at 16:27
  • Re, "I'm talking about...bad design." Don't talk about it. _Show_ it to us. I happen to know that calling `sleep()` won't cause a program to use any extra memory, but maybe some strategy that you have tried—some strategy in which sleep() happens to play a part—uses memory. But nobody can comment on that unless you show it to us. – Solomon Slow May 18 '22 at 17:03

0 Answers0