记录一下ReentrantLock + Condition的组合使用

/**
 * 可重入锁 + Condition组合测试
 * <p>
 * 实现张三说1 ,李四说2 ,王五说3,赵六说4,然后轮询
 *
 * @author xiaofeifei
 * @date 2020-03-12
 * @since
 */
@FieldDefaults(level = AccessLevel.PRIVATE)
public class ReentrantLockTest {

    ReentrantLock lock;

    Condition condition1;

    Condition condition2;

    Condition condition3;

    Condition condition4;

    static AtomicInteger count;

    public ReentrantLockTest() {
        lock = new ReentrantLock();
        condition1 = lock.newCondition();
        condition2 = lock.newCondition();
        condition3 = lock.newCondition();
        condition4 = lock.newCondition();
        count = new AtomicInteger(0);
    }


    public void say1() throws InterruptedException {
        try {
            lock.lock();
            System.out.println(Thread.currentThread().getName() + " 说 => 1");
            condition2.signal();
        } finally {
            count.getAndIncrement();
            condition1.await();
            // lock.unlock();
        }
    }

    public void say2() throws InterruptedException {
        try {
            lock.lock();
            System.out.println(Thread.currentThread().getName() + " 说 => 2");
            condition3.signal();
        } finally {
            count.getAndIncrement();
            condition2.await();
            // lock.unlock();
        }
    }

    public void say3() throws InterruptedException {
        try {
            lock.lock();
            System.out.println(Thread.currentThread().getName() + " 说 => 3");
            condition4.signal();
        } finally {
            count.getAndIncrement();
            condition3.await();
            //   lock.unlock();
        }
    }

    public void say4() throws InterruptedException {
        try {
            lock.lock();
            System.out.println(Thread.currentThread().getName() + " 说 => 4");
            condition1.signal();
        } finally {
            count.getAndIncrement();
            // 将当前线程加入到condition4的等待队列中,然后尝试释放锁,自身陷入等待
            condition4.await();
            //  lock.unlock();
        }
    }

    public static void main(String[] args) {
        ReentrantLockTest test = new ReentrantLockTest();

        AtomicInteger integer = new AtomicInteger();
        ThreadPoolExecutor threadPoolExecutor = new ThreadPoolExecutor(4, 4, 60,
                TimeUnit.SECONDS, new LinkedBlockingQueue<>(100), (runnable) ->
                new Thread(Thread.currentThread().getThreadGroup(), runnable, "飞飞线程池" + integer.incrementAndGet()));


        threadPoolExecutor.execute(() -> {
            try {
                while (true) {
                    TimeUnit.SECONDS.sleep(1);
                    if (count.get() % 4 == 1) test.say2();

                }
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        });

        threadPoolExecutor.execute(() -> {
            try {
                while (true) {
                    TimeUnit.SECONDS.sleep(1);
                    if (count.get() % 4 == 3) test.say4();
                }

            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        });

        threadPoolExecutor.execute(() -> {
            try {
                while (true) {
                    TimeUnit.SECONDS.sleep(1);
                    if (count.get() % 4 == 2) test.say3();
                }
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        });

        threadPoolExecutor.execute(() -> {
            try {
                while (true) {
                    TimeUnit.SECONDS.sleep(1);
                    if (count.get() % 4 == 0) test.say1();
                }
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        });
    }

}

 


版权声明:本文为qq_38796327原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接和本声明。