multi-thread(六)ReentrantLock,Condition

/**
 * 3个线程,依次执行
 * @author timeriver.wang
 * @date 2014-03-10 2:06:19 PM
 */
public class ThreeThreadCommunication {
    public static void main( String[] args ) {
        final Bunnies bunnies = new Bunnies();
        new Thread( new Runnable() {
            public void run() {
                for ( int i = 0; i < 4; i++ ) {
                    bunnies.sub2( i );
                }
            }
        } ).start();
        new Thread( new Runnable() {
            public void run() {
                for ( int i = 0; i < 4; i++ ) {
                    bunnies.sub3( i );
                }
            }
        } ).start();
        // main方法本身就是主线程
        for ( int i = 0; i < 4; i++ ) {
            bunnies.main( i );
        }
    }

    static class Bunnies {
        Lock lock = new ReentrantLock();

        Condition condition1 = lock.newCondition();

        Condition condition2 = lock.newCondition();

        Condition condition3 = lock.newCondition();
        // 对flag共享数据,已经加锁保护了
        private int flag = 1;

        public void sub2( int i ) {
            lock.lock();
            try {
                while ( flag != 2 ) {
                    try {
                        condition2.await();
                    }
                    catch ( InterruptedException e ) {
                        e.printStackTrace();
                    }
                }
                for ( int j = 0; j < 4; j++ ) {
                    System.out.println( Thread.currentThread() + "sequence : " + j + " loop of : " + i );
                }
                flag = 3;
                condition3.signal();
            }
            finally {
                lock.unlock();
            }
        }

        public void sub3( int i ) {
            lock.lock();
            try {
                while ( flag != 3 ) {
                    try {
                        condition3.await();
                    }
                    catch ( InterruptedException e ) {
                        e.printStackTrace();
                    }
                }
                for ( int j = 0; j < 4; j++ ) {
                    System.out.println( Thread.currentThread() + "sequence : " + j + " loop of : " + i );
                }
                flag = 1;
                condition1.signal();
            }
            finally {
                lock.unlock();
            }
        }

        public void main( int i ) {
            lock.lock();
            try {
                while ( flag != 1 ) {
                    try {
                        condition1.await();
                    }
                    catch ( InterruptedException e ) {
                        e.printStackTrace();
                    }
                }
                for ( int j = 0; j < 4; j++ ) {
                    System.out.println( Thread.currentThread() + "sequence : " + j + " loop of : " + i );
                }
                flag = 2;
                condition2.signal();
            }
            finally {
                lock.unlock();
            }
        }
    }
}

猜你喜欢

转载自luckywnj.iteye.com/blog/2028854