package com.njust.test.learn;

import java.util.concurrent.locks.Condition;
import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;

public class LockConditionDemo
{
    private static final int COUNT = 50;
    
    public static void main(String[] args)
    {
        Manage manage = new Manage();
        
        new Thread(() -> {
            for (int i = 1; i <= COUNT; i++)
            {
                manage.subDo(i);
            }
        }).start();
        
        for (int i = 1; i <= COUNT; i++)
        {
            manage.mainDo(i);
        }
    }
    
    static class Manage
    {
        private static final int SUB_INTERVAL = 10;
        
        private static final int MAIN_INTERVAL = 50;
        
        private boolean subTurn = true;
        
        private Lock lock = new ReentrantLock();
        
        private Condition condition = lock.newCondition();
        
        public void subDo(int index)
        {
            lock.lock();
            
            try
            {
                while (!subTurn)
                {
                    try
                    {
                        //this.wait();
                        condition.await();
                    }
                    catch (Exception e)
                    {
                        e.printStackTrace();
                    }
                }
                
                for (int i = 1; i <= SUB_INTERVAL; i++)
                {
                    System.out.println(String.format("-> Sub[%s]: %s/%s", index, i, SUB_INTERVAL));
                }
                
                subTurn = false;
                //this.notify();
                condition.signal();
            }
            finally
            {
                lock.unlock();
            }
            
        }
        
        public void mainDo(int index)
        {
            lock.lock();
            
            try
            {
                while (subTurn)
                {
                    try
                    {
                        //this.wait();
                        condition.await();
                    }
                    catch (Exception e)
                    {
                        e.printStackTrace();
                    }
                }
                
                for (int i = 1; i <= MAIN_INTERVAL; i++)
                {
                    System.out.println(String.format("=====> Main[%s]: %s/%s", index, i, MAIN_INTERVAL));
                }
                
                subTurn = true;
                //this.notify();
                condition.signal();
            }
            finally
            {
                lock.unlock();
            }
            
        }
    }
}



@@ -159,16 +159,77 @@
 tLock;%0A%0A
+/**%0A * %E4%B8%A4%E4%B8%AA%E7%BA%BF%E7%A8%8B%E8%BD%AE%E6%B5%81%E6%95%B0%E6%95%B0%0A * %E7%BA%BF%E7%A8%8B1%EF%BC%8C%E4%BB%8E1%E6%95%B0%E6%95%B0%E5%88%B010%0A * %E7%BA%BF%E7%A8%8B2%EF%BC%8C%E4%BB%8E1%E6%95%B0%E6%95%B0%E5%88%B050%0A * %E4%B8%A4%E4%B8%AA%E7%BA%BF%E7%A8%8B%E8%BD%AE%E6%B5%81%E6%95%B0%0A */%0A
 public c

Update LockConditionDemo.java