1
\$\begingroup\$

It's a part of code where two Threads ("priests") talk to each other. One asks and waits for the second to answer - then something happens depending on what has been answered by second thread.

I have two simple questions:

  1. Regarding structuring code as MVP: I am not sure it's valid to keep all the thread logic in the same file where psvm is. Should I rather move it in separate files? What's the best approach?
  2. I know that garbage collection in Java is automated, yet after reading this I've decided it would be polite to JVM to null all the objects I don't need to hint it on that they might be collected. Is it even sane? Or just extra hassle?

This is the code:

package View;

class Genesis {

private boolean turnToTalk, agreed;
private Priest priestOne, priestTwo;

public static void main(String[] args) {
    Genesis genesis = new Genesis();
    genesis.PriestsTalk();
}

private void PriestsTalk() {
    turnToTalk = true;
    Thread0 thread0 = new Thread0();
    thread0.run();
}

class Thread0 implements Runnable {
    @Override
    public void run() {
        turnToTalk = false;

        Thread1 thread1 = new Thread1();
        thread1.run();

        while (!turnToTalk) {
            try {
                wait();
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }

        // after thread woke up
        if (agreed) {
            // do some work
            } else {
                // do some work
            }            
        } else {
            PriestsTalk();
        }
    }
}

class Thread1 implements Runnable {
    @Override
    public void run() {
        synchronized (this) {
            while (turnToTalk) {
                try {
                    wait();
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }

            Random r = new Random();
            int i = r.nextInt(2);
            String answer =
                    i != 0 ? "Yes" : "No";

            if (answer.equals("Yes")) {
                agreed = true;
                turnToTalk = true;
                r = null;
                answer = null;
                notify();
            } else {
                agreed = false;
                turnToTalk = true;
                r = null;
                answer = null;
                notify();
            }
        }
    }
}

Full code is here, just 4 files.

\$\endgroup\$
  • \$\begingroup\$ Does this code work? There are some // do some work placeholder comments. \$\endgroup\$ – 200_success Feb 26 '17 at 12:57
  • \$\begingroup\$ Dear @200_success it does work. I just got rid of all the parts that doesn't contribute to the questions to ease readability. \$\endgroup\$ – Artem Dobrovinskiy Feb 27 '17 at 7:12

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.