3
\$\begingroup\$

Below I am giving you my code which I wrote to print even number using even thread and odd number using odd thread sequentially.

Could anyone please validate my code whether it is correct way of doing it or not? I am getting correct result, but I just want to make sure whether I am doing it in correct way or not.

public class PrintNumberUsingThread {

    public static void main(String[] args) {
        PrintNumberUsingThread example = new PrintNumberUsingThread();
        PNThread pnThread = example.new PNThread(10);
        new Thread(pnThread).start();
        new Thread(pnThread).start();
    }
    class PNThread implements Runnable {

        private int number = 0;
        private int maxNumber;
        public PNThread(int maxNumber) {
            this.maxNumber = maxNumber;
        }
        @Override
        public void run() {

            while (number <= maxNumber) {
                synchronized (this) {

                    String threadName = Thread.currentThread().getName();
                    if (number <= maxNumber) {

                        boolean even = threadName.contains("0") && number % 2 == 0;
                        boolean odd = threadName.contains("1") && number % 2 != 0;
                        if (even || odd) {

                            System.out.println(threadName + "-" + number++);
                            this.notifyAll();
                        } else {
                            try {
                                this.wait();
                            } catch (InterruptedException e) {
                                e.printStackTrace();
                            }
                        }
                    }
                }
            }
        }
    }
}
\$\endgroup\$
  • 2
    \$\begingroup\$ Can you post the exact assignment text? \$\endgroup\$ – TorbenPutkonen Nov 13 '19 at 7:08
  • \$\begingroup\$ Why do you need synchronized (this) in the thread ? You don't have concurrent access to the run() method. \$\endgroup\$ – rpc1 Nov 13 '19 at 8:00
  • \$\begingroup\$ @rpc1 I need synchronization because I am waiting a thread if that is not satisfying a condition (the else block) also I am notifying all the other waiting threads when condition satisfies. \$\endgroup\$ – Arghya Pal Nov 13 '19 at 8:06
  • \$\begingroup\$ @TorbenPutkonen What do you mean by exacct assignment text? Do you want me to give what exactly I want to do in this program? If yes, then I want to print odd numbers using odd thread and even numbers using even thread. \$\endgroup\$ – Arghya Pal Nov 13 '19 at 8:10
  • \$\begingroup\$ It's just that this looks like homework, if so, this works best when you give us the exact homework description. \$\endgroup\$ – konijn Nov 13 '19 at 13:36
2
\$\begingroup\$

If the only requirement is to have two threads, one printing even numbers and other printing odd numbers, then your solution is unnecessarily complicated. The fact that you share the same data model for both threads forces you to synchronize access to the object making the solution unnecessarily complicated. The assignment did not require numbers being printed in sequence, so the coordination between the threads is unnecessary.

public class Counter implements Runnable {
    private final int start;
    private final int end;
    private final int increment;
    ... add boilerplate constructor ...
    public void run() {
        for (int i = start; i <= end; i += increment) {
            System.out.println(i);
        }
    }
}

Start threads:

new Thread(new Counter(1, 10, 2)).start();
new Thread(new Counter(2, 10, 2)).start();
| improve this answer | |
\$\endgroup\$
  • \$\begingroup\$ Yes got it, but I am sorry I forgot to mention that I want to print in in a sequence. \$\endgroup\$ – Arghya Pal Nov 13 '19 at 9:04

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.