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

```
```
This will do:
import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.atomic.AtomicInteger;

 public class Threads implements Runnable{

    static AtomicInteger  x=new AtomicInteger(1);
    public  void generate()
    {
        //System.out.println("In generate");
      synchronized(x){
        if(x.get()%2==0)
            System.out.println(x+" is EVEN");
        else
            System.out.println(x+" is ODD");
        x.incrementAndGet();
        }

    }
    @Override
    public synchronized void run() {        
        //System.out.println("In run");
        generate();
    }

    public static void main(String[] args) {
        //System.out.println("In generateThreads");
            ExecutorService pool=Executors.newFixedThreadPool(10);
            for(int i=0;i<2;i++) // two threads
            pool.submit(new Threads());
            pool.shutdown();
            while (!pool.isTerminated()) {

            }
         //   System.out.println("DONEs");

    }

}

```