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

```
Your question is a little confusing because it looks like you want the output in sequential ordering.

It doesn't make sense to use threads in that case because they would be constantly coordinating with each other to determine whose turn it is to output their current number.

If you don't care about ordering (e.g. you might get 1,2,3,5,4,6,8.7...), then it might make sense.

```
public class Test
{
    private static final int NOT_APPLICABLE = 1;

    private final ExecutorService executor;

    public Test()
    {
        BlockingQueue<Runnable> queue = new ArrayBlockingQueue<Runnable>(2);

        executor = new ThreadPoolExecutor(2, 2, NOT_APPLICABLE, TimeUnit.SECONDS, queue);
    }

    public void submitTask(Runnable task)
    {
        executor.submit(task);
    }

    private static class Counter implements Runnable
    {
        private int counter;

        public Counter(int start)
        {
            this.counter = start;
        }

        @Override
        public void run()
        {
            while (true)
            {
                System.out.println(counter);
                counter += 2;
            }
        }

    }
    public static void main(String[] args)
    {
        Runnable odd = new Counter(1);
        Runnable even = new Counter(2);
        Test app = new Test();
        app.submitTask(odd);
        app.submitTask(even);
    }
}

```