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

```
```
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.atomic.AtomicBoolean;
import java.util.concurrent.atomic.AtomicInteger;
import java.util.concurrent.locks.Condition;
import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;

public class GenerateEvenOddNumberTest {

    public static void main(String[] args) {
        GenerateNumber GenNum = new GenerateNumber();
        ExecutorService es =    Executors.newFixedThreadPool(2);
        es.execute(GenNum);
        es.execute(GenNum);
    }

}

class GenerateNumber implements Runnable{

    private static final AtomicInteger nextId = new AtomicInteger(0);   

    // Thread local variable containing each thread's ID
    private static final ThreadLocal<Integer> count =
        new ThreadLocal<Integer>() {
            @Override protected Integer initialValue() {
                return nextId.getAndIncrement();
        }
    };

    private static final AtomicBoolean bool = new AtomicBoolean(true);  
      // Thread local variable containing each thread's ID
    private static final ThreadLocal<Boolean> even =
        new ThreadLocal<Boolean>() {
            @Override protected Boolean initialValue() {
                return bool.getAndSet(false)    ;
        }
    };  

    boolean isOdd = false;  
    Lock lock = new ReentrantLock();
    Condition checkOdd = lock.newCondition();
    Condition checkEven = lock.newCondition();

    public void printEvenNumber(){
        lock.lock();
        try {
            while(count.get()<500){
                while(isOdd){
                    try {
                        checkEven.await();
                    } catch (InterruptedException e) {
                        // TODO Auto-generated catch block
                        e.printStackTrace();
                    }
                }
                System.out.println("Thread Name "+Thread.currentThread().getName()+" "+count.get());
                count.set(count.get()+2);
                isOdd = true;
                checkOdd.signal();
            }
        } finally {
            lock.unlock();
        }
    }

    public void printOddNumber(){
        lock.lock();
        try {       
            while(count.get()<500){
                while(!isOdd){
                    try {
                        checkOdd.await();
                    } catch (InterruptedException e) {
                        // TODO Auto-generated catch block
                        e.printStackTrace();
                    }
                }
                System.out.println("Thread Name "+Thread.currentThread().getName()+" "+count.get());
                count.set(count.get()+2);
                isOdd = false;
                checkEven.signal();
            }
        } finally {
            lock.unlock();
        }           
    }

    @Override
    public void run() {
        if(even.get()){
            printEvenNumber();
        }
        else{
            printOddNumber();
        }
    }
}

```