美文网首页J2ee
3个线程按顺序执行

3个线程按顺序执行

作者: 超人TIGA | 来源:发表于2022-03-24 10:42 被阅读0次
    public class MyService
    {
        private int flag = 1;
        
        public synchronized void printA(){
            
            while (flag != 1)
            {
                try
                {
                    this.wait();
                }
                catch (InterruptedException e)
                {
                    e.printStackTrace();
                }
            }
            System.out.print(Thread.currentThread().getName());
            flag = 2;
            this.notifyAll();
        }
        public synchronized void printB(){
            while (flag != 2)
            {
                try
                {
                    this.wait();
                }
                catch (InterruptedException e)
                {
                    e.printStackTrace();
                }
            }
            System.out.print(Thread.currentThread().getName());
            flag = 3;
            this.notifyAll();
        }
        public synchronized void printC(){
            while (flag != 3)
            {
                try
                {
                    this.wait();
                }
                catch (InterruptedException e)
                {
                    e.printStackTrace();
                }
            }
            System.out.print(Thread.currentThread().getName());
            flag = 1;
            this.notifyAll();
        }
    }
    

    测试代码:

    package testABC;
    
    public class TestMain
    {
        public static void main(String[] args)
        {
    //编写一个程序,启动三个线程,三个线程的ID分别是A,B,C;,每个线程将自己的ID值在屏幕上打印5遍,打印顺序是ABCABC...
    //        MyService service = new MyService();
            MyService2 service = new MyService2();
            
            Thread A = new Thread(new Runnable()
            {
                @Override
                public void run()
                {
                    for (int i = 0; i < 5; i++)
                    {
                        service.printA();
                    }
                }
            });
            A.setName("A");
            Thread B = new Thread(new Runnable()
            {
                @Override
                public void run()
                {
                    for (int i = 0; i < 5; i++)
                    {
                        service.printB();
                    }
                }
            });
            B.setName("B");
            Thread C = new Thread(new Runnable()
            {
                @Override
                public void run()
                {
                    for (int i = 0; i < 5; i++)
                    {
                        service.printC();
                    }
                }
            });
            C.setName("C");
            
            A.start();
            B.start();
            C.start();
        }
    }
    

    使用Lock实现:

    import java.util.concurrent.locks.Condition;
    import java.util.concurrent.locks.Lock;
    import java.util.concurrent.locks.ReentrantLock;
    
    public class MyService2
    {
        private int flag = 1;
        private Lock lock = new ReentrantLock();
        private Condition conditionA = lock.newCondition();
        private Condition conditionB = lock.newCondition();
        private Condition conditionC = lock.newCondition();
    
        public void printA()
        {
            try
            {
                lock.lock();
                if (flag != 1)
                {
                    try
                    {
                        conditionA.await();
                    }
                    catch (InterruptedException e)
                    {
                        e.printStackTrace();
                    }
                }
                System.out.print(Thread.currentThread().getName());
                flag = 2;
                conditionB.signal();
            }
            finally
            {
                lock.unlock();
            }
    
        }
    
        public void printB()
        {
            try
            {
                lock.lock();
                if (flag != 2)
                {
                    try
                    {
                        conditionB.await();
                    }
                    catch (InterruptedException e)
                    {
                        e.printStackTrace();
                    }
                }
                System.out.print(Thread.currentThread().getName());
                flag = 3;
                conditionC.signal();
            }
            finally
            {
                lock.unlock();
            }
    
        }
    
        public void printC()
        {
            try
            {
                lock.lock();
                if (flag != 3)
                {
                    try
                    {
                        conditionC.await();
                    }
                    catch (InterruptedException e)
                    {
                        e.printStackTrace();
                    }
                }
                System.out.print(Thread.currentThread().getName());
                flag = 1;
                conditionA.signal();
            }
            finally
            {
                lock.unlock();
            }
        }
    }
    

    相关文章

      网友评论

        本文标题:3个线程按顺序执行

        本文链接:https://www.haomeiwen.com/subject/gkkydrtx.html