跳到主要内容

r/javahelp


Why does adding another condition in my while loop force the turtle to win? (Multithreading)
Why does adding another condition in my while loop force the turtle to win? (Multithreading)
Homework

i'm currently learning about multithreading for my java class and part of the assignment requires me to race two threads (turtle and a hare) together and force one of them to win using the wait() and notify() methods (in this case the turtle thread).

public class Race {

    private Integer numberOfLaps;
    private Integer lapToStart = 2;
    private boolean isWinning = true;

    public synchronized void winner(Integer numberOfLaps) {
        while (!isWinning) {
            try {
                wait();
            }
            catch (InterruptedException e){}
        }

        this.numberOfLaps = numberOfLaps;
        System.
out
.println("Tortoise is on lap: " + numberOfLaps);

        isWinning = false;
        notify();
    }

    public synchronized void loser() {
        while (isWinning && numberOfLaps < lapToStart) {
            try {
                wait();
            }
            catch (InterruptedException e){}
        }

        System.
out
.println("Hare is on lap: " + numberOfLaps);
        isWinning = true;
        notify();

    }
} public class Race {

    private Integer numberOfLaps;
    private Integer lapToStart = 2;
    private boolean isWinning = true;

    public synchronized void winner(Integer numberOfLaps) {
        while (!isWinning) {
            try {
                wait();
            }
            catch (InterruptedException e){}
        }

        this.numberOfLaps = numberOfLaps;
        System.out.println("Tortoise is on lap: " + numberOfLaps);

        isWinning = false;
        notify();
    }

    public synchronized void loser() {
        while (isWinning) {
            try {
                wait();
            }
            catch (InterruptedException e){}
        }

        System.out.println("Hare is on lap: " + numberOfLaps);
        isWinning = true;
        notify();

    }
}

Output:

Tortoise is on lap: 1

Hare is on lap: 1

Tortoise is on lap: 2

Hare is on lap: 2

Tortoise is on lap: 3

Hare is on lap: 3

Tortoise is on lap: 4

Hare is on lap: 4

Tortoise is on lap: 5

Hare is on lap: 5

Tortoise is on lap: 6

Hare is on lap: 6

Tortoise is on lap: 7

Hare is on lap: 7

Tortoise is on lap: 8

Hare is on lap: 8

Tortoise is on lap: 9

Hare is on lap: 9

Tortoise is on lap: 10

Hare is on lap: 10

If this prints out first before the hare finishes, the turtle has won! Congrats

If in the while loop for the loser method is only the volatile boolean, both the turtle and the hare finish at the exact same time. however, if i add another condition to it

public synchronized void loser() {
    while (isWinning && numberOfLaps < lapToStart) {
        try {
            wait();
        }
        catch (InterruptedException e){}
    }public synchronized void loser() {
    while (isWinning && numberOfLaps < lapToStart) {
        try {
            wait();
        }
        catch (InterruptedException e){}
    }

the output becomes:

Tortoise is on lap: 1

Hare is on lap: 1

Tortoise is on lap: 2

Hare is on lap: 2

Tortoise is on lap: 3

Hare is on lap: 3

Hare is on lap: 3

Tortoise is on lap: 4

Hare is on lap: 4

Tortoise is on lap: 5

Hare is on lap: 5

Tortoise is on lap: 6

Hare is on lap: 6

Tortoise is on lap: 7

Hare is on lap: 7

Tortoise is on lap: 8

Hare is on lap: 8

Tortoise is on lap: 9

Hare is on lap: 9

Tortoise is on lap: 10

If this prints out first before the hare finishes, the turtle has won! Congrats

now whenever the code is ran, the hare always lags behind at some point and the turtle finishes first. why does the code suddenly function as i intended when i add a second condition to the while loop? also, this is my first time posting something code related. i apologize if i'm missing anything. any help would be appreciated. thank you :)


广告:Finally, a phone that knows who to let in. Try HiLight on the Google Pixel 11 Pro
Finally, a phone that knows who to let in. Try HiLight on the Google Pixel 11 Pro
media poster


Good Documentation For JVM Bytecode?
Good Documentation For JVM Bytecode?

I really liked the idea of an intermediate bytecode language when I first learned that Java transpiles to one, so I tried to learn about it and got pretty far (using a hexeditor to mess around was pretty painful, though). I stopped because there isn't a lot of document for it like there is for Java, the best resources are I believe docs.oracle.com/javase/specs/jvms/se7/html/jvms-4.html and docs.oracle.com/javase/specs/jvms/se7/html/jvms-6.html, but I want to know if there are any others partly because these are kind of verbose and partly because it's just better to have more than two.

I know I won't ever really need to know any of this, and I don't intend to use any of this knowledge for making classes by hand, but I believe it would still be helpful in a milder sense to understand what the class files are "literally" doing. It helped with C


ZGC vs Shenandoah: what are the fundamental differences today?
ZGC vs Shenandoah: what are the fundamental differences today?

I'm trying to better understand the differences between ZGC and Shenandoah, beyond the usual "both are low-latency concurrent collectors" explanation.

From a JVM/runtime perspective, what are the fundamental architectural differences between them?

In particular:

  • How do their barriers differ (load/store barriers, SATB, colored pointers, etc.)?

  • ZGC uses forward table and shenandoah no, what does it imply ?