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: 
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);
    }
}

```
/\* Using Semaphore was the easiet way to print odd-even series \*/

```
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.Semaphore;

class Even implements Runnable {

    private Semaphore s1;
    private Semaphore s2;
    private static volatile int num = 0;

    Even(Semaphore s1, Semaphore s2) {
        this.s1 = s1;
        this.s2 = s2;
    }

    @Override
    public void run() {
        synchronized (s2) {
            while (num < 99) {
                try {
                    s1.acquire();
                    System.out.print(" " + num);
                    num += 2;
                    s2.release();
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        }
    }
}

class Odd implements Runnable {

    private Semaphore s1;
    private Semaphore s2;
    private static volatile int num = 1;

    Odd(Semaphore s1, Semaphore s2) {
        this.s1 = s1;
        this.s2 = s2;
    }

    @Override
    public void run() {
        synchronized (s1) {
            while (num < 100) {
                try {
                    s2.acquire();
                    System.out.print(" " + num);
                    num += 2;
                    s1.release();
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        }
    }
}

public class ExecOddEvenPrint {

    public static void main(String[] args) {

        ExecutorService exec = Executors.newFixedThreadPool(2);
        Semaphore s1 = new Semaphore(1);
        Semaphore s2 = new Semaphore(0);

        exec.execute(new Even(s1, s2));
        exec.execute(new Odd(s1, s2));

    }

}

```