Question: 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](https://stackoverflow.com/questions/2742520/program-to-print-odd-numbers-and-even-numbers-on-seperate-threads) but that is in C . I want a Java implementation here .

Answer: 
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

```
Some busy waiting code, the simplest.

```
package t1;

import java.util.concurrent.atomic.AtomicInteger;

public class Redx implements Runnable{
    private final static int MAX = 99;
    final AtomicInteger next;
    final int odd;

    public Redx(AtomicInteger next, int odd) {
        super();
        this.next = next;
        this.odd = odd;
    }

    public void run(){
        for(;;){
            int n = next.get();
            if (n > MAX)
                break;
            if ((n&1)==odd)
                continue;           

            System.out.print(n+", ");
            if ((n & 0x1F)==0x1F)//new line can be skipped
                System.out.println();
            next.lazySet(n+1);

        }
    }
    public static void main(String[] args) {
        final AtomicInteger next = new AtomicInteger(0);
        Redx x0 = new Redx(next, 0);
        Redx x1 = new Redx(next, 1);
        new Thread(x0).start();
        new Thread(x1).start();
        for(;next.get()<=MAX;)
                Thread.yield();

        System.out.println();
        System.out.println("Done!");
    }

}

```