How to make sure two threads printing even odd numbers maintain even first then odd order for this implementation?

后端 未结 3 1568
独厮守ぢ
独厮守ぢ 2021-01-16 01:24

I have created two runnable jobs: PrintEvenNumbersJob and PrintOddNumbersJob and spawned two threads to execute these jobs. This seems to work perfectly fine! But I smell so

3条回答
  •  隐瞒了意图╮
    2021-01-16 01:53

    I tried and tested this code. It works using Semaphore

    public class TestSemaphore
    {
    
        public static void main(String[] args)
            throws Exception
        {
            AtomicInteger count = new AtomicInteger();
            Semaphore s = new Semaphore(1, true);
            Semaphore t = new Semaphore(1, true);
    
            OddNumberThread oThread = new OddNumberThread(count, s, t);
            EvenNumberThread eThread = new EvenNumberThread(count, s, t);
    
            eThread.start();
            oThread.start();
        }
    
        static class EvenNumberThread
            extends Thread
        {
            private AtomicInteger count;
    
            private Semaphore s, t;
    
            public EvenNumberThread(AtomicInteger pCount, Semaphore pS, Semaphore pT)
            {
                super("Even");
                count = pCount;
                s = pS;
                t = pT;
            }
    
            @Override
            public void run()
            {
                // Make this thread wait until even thread starts, Order will be incorrect if removed these lines.
                s.acquireUninterruptibly();
                while (count.intValue() <= 10)
                {
                    try
                    {
                        // Double checking to make it work
                        s.acquireUninterruptibly();
    
                        System.out.println(getName() + " " + count.getAndIncrement());
                    }
                    finally
                    {
                        t.release();
                    }
                }
            }
        }
    
        static class OddNumberThread
            extends Thread
        {
            private AtomicInteger count;
    
            private Semaphore s, t;
    
            public OddNumberThread(AtomicInteger pCount, Semaphore pS, Semaphore pT)
            {
                super("Odd");
                count = pCount;
                s = pS;
                t = pT;
            }
    
            @Override
            public void run()
            {
                // Start this thread first and start printing, Order will be incorrect if removed these lines.
                t.acquireUninterruptibly();
                s.release();
    
                while (count.intValue() <= 10)
                {
                    try
                    {
                        t.acquireUninterruptibly();
    
                        System.out.println(getName() + " " + count.getAndIncrement());
                    }
                    finally
                    {
                        s.release();
                    }
                }
            }
        }
    }
    

提交回复
热议问题