Search code examples
javamultithreadingalgorithmjava.util.concurrent

Java: How to print odd and even numbers from 2 separate threads using Executor framework


I want an algorithm that prints odd numbers and even numbers in separate threads . The output should be sequential 1,2,3.4 ,..... Executor framework can be used here . I have seen a related question on SO but that is in C . I want a Java implementation here .


Solution

  • Its a modified version of jasons:

    import java.util.concurrent.Executor;
    import java.util.concurrent.Executors;
    import java.util.concurrent.atomic.AtomicInteger;
    
    public class Test {
    
        public static void main(String[] args){
        final int max = 100;
        final AtomicInteger i = new AtomicInteger(0);
        Executor dd = Executors.newFixedThreadPool(2);
    
        final Object lock = new Object();
    
        dd.execute(new Runnable() {
            @Override
            public void run() {
                while (i.get() < max) {
                    if (i.get() % 2 == 0) {
                        System.out.print(" " + i.getAndAdd(1));
    
                        synchronized(lock){
                            lock.notify();
                        }
                    }else{
                        synchronized(lock){
                            try {
                                lock.wait();
                            } catch (InterruptedException e) {
                                // TODO Auto-generated catch block
                                e.printStackTrace();
                            }
                        }
                    }
                }
            }
        });
        dd.execute(new Runnable() {
            @Override
            public void run() {
                while (i.get() < max) {
                    if (i.get() % 2 != 0) {
                        System.out.print(" " + i.getAndAdd(1));
    
                        synchronized(lock){
                            lock.notify();
                        }
                    }else{
                        synchronized(lock){
                            try {
                                lock.wait();
                            } catch (InterruptedException e) {
                                // TODO Auto-generated catch block
                                e.printStackTrace();
                            }
                        }
                    }
                }
            }
        });
        do {
            try {
                Thread.currentThread().sleep(1000);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        } while (i.get() != max);
        System.out.println("\nDone");
    }
    }
    

    Disclaimer: its not the best solution, and for sure not the fastest, but it produces the right output.

    Thats the output:

     0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99
    Done