欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

Condition 测试记录

程序员文章站 2024-02-17 23:34:28
...

Test01

/**
 * main开启子线程
 * main主线程休眠3S
 * ...子线程开始, 尝试获取锁
 * ...子线程获取锁成功
 * ...子线程等待
 * main尝试获取锁
 * main线程获取锁成功
 * main唤醒子线程
 * main线程锁释放
 * ...子线程等待结束(被唤醒)
 * ...子线程休眠3S
 */
public class Test01 {

    /** 锁对象 */
    private static Lock lock = new ReentrantLock();

    /** condition 对象 */
    private static Condition condition = lock.newCondition();

    /** 任务 */
    static class Task implements Runnable {
        @Override
        public void run() {
            System.out.println("...子线程开始, 尝试获取锁");
            lock.lock();
            try {
                System.out.println("...子线程获取锁成功");

                System.out.println("...子线程等待");
                condition.await();

                System.out.println("...子线程等待结束(被唤醒)");

                System.out.println("...子线程休眠3S");
                Thread.sleep(3000);

            } catch (Exception e) {
                System.out.println("...子线程异常");
            } finally {
                lock.unlock();
                System.out.println("...子线程锁释放");
            }
        }
    }

    public static void main(String[] args) throws InterruptedException {
        Task task = new Task();
        System.out.println("main开启子线程");
        new Thread(task).start();

        System.out.println("main主线程休眠3S");
        Thread.sleep(3000);



        System.out.println("main尝试获取锁");
        lock.lock();
        try {
            System.out.println("main线程获取锁成功");

            System.out.println("main唤醒子线程");
            condition.signal();
        } finally {
            lock.unlock();
            System.out.println("main线程锁释放");
        }

    }

}

Test02

/**
 * main开启子线程
 * main主线程休眠3S(1)
 *
 * TaskA...子线程开始, 尝试获取锁
 * TaskA...子线程获取锁成功
 * TaskA...子线程休眠3S(1)
 *
 * TaskB...子线程开始, 尝试获取锁
 * TaskA...子线程等待
 * main尝试获取锁TaskA
 * TaskB...子线程获取锁成功
 * TaskB...子线程休眠3S(1)
 *
 * TaskB...子线程等待
 * main线程获取锁成功TaskA
 * main唤醒子线程TaskA
 * main线程锁释放TaskA
 * main主线程休眠3S(2)
 *
 * TaskA...子线程等待结束(被唤醒)
 * TaskA...子线程休眠3S(2)
 */
public class Test02 {

    /** 锁对象 */
    private static ReentrantLock lock = new ReentrantLock();

    /** 两个相同锁对象的 condition 对象 */
    private static Condition conditionA = lock.newCondition();
    private static Condition conditionB = lock.newCondition();

    /** 任务 */
    static class TaskA implements Runnable {
        @Override
        public void run() {
            System.out.println("TaskA...子线程开始, 尝试获取锁");
            lock.lock();
            try {
                System.out.println("TaskA...子线程获取锁成功");

                System.out.println("TaskA...子线程休眠3S(1)\n");
                Thread.sleep(3000);

                System.out.println("TaskA...子线程等待");
                conditionA.await();

                System.out.println("TaskA...子线程等待结束(被唤醒)");

                System.out.println("TaskA...子线程休眠3S(2)\n");
                Thread.sleep(3000);

            } catch (Exception e) {
                System.out.println("TaskA...子线程异常");
            } finally {
                lock.unlock();
                System.out.println("TaskA...子线程锁释放");
            }
        }
    }

    /** 任务 */
    static class TaskB implements Runnable {
        @Override
        public void run() {
            System.out.println("TaskB...子线程开始, 尝试获取锁");
            lock.lock();
            try {
                System.out.println("TaskB...子线程获取锁成功");

                System.out.println("TaskB...子线程休眠3S(1)\n");
                Thread.sleep(3000);

                System.out.println("TaskB...子线程等待");
                conditionB.await();

                System.out.println("TaskB...子线程等待结束(被唤醒)");

                System.out.println("TaskB...子线程休眠3S(2)\n");
                Thread.sleep(3000);

            } catch (Exception e) {
                System.out.println("TaskB...子线程异常");
            } finally {
                lock.unlock();
                System.out.println("TaskB...子线程锁释放");
            }
        }
    }

    public static void main(String[] args) throws InterruptedException {
        TaskA taskA = new TaskA();
        TaskB taskB = new TaskB();
        System.out.println("main开启子线程");
        new Thread(taskA).start();
        new Thread(taskB).start();

        System.out.println("main主线程休眠3S(1)\n");
        Thread.sleep(3000);

        System.out.println("main尝试获取锁TaskA");
        lock.lock();
        try {
            System.out.println("main线程获取锁成功TaskA");

            System.out.println("main唤醒子线程TaskA");
            conditionA.signal();

        } finally {
            lock.unlock();
            System.out.println("main线程锁释放TaskA");
        }

        System.out.println("main主线程休眠3S(2)\n");
        Thread.sleep(3000);

        lock.lock();
        try {
            System.out.println("main线程获取锁成功TaskB");

            System.out.println("main唤醒子线程TaskB");
            conditionA.signal();

            System.out.println("main唤醒子线程TaskB");
            conditionB.signal();

        } finally {
            lock.unlock();
            System.out.println("main线程锁释放TaskB");
        }

    }

}

Test03

/**
 * TaskA和TaskB进行锁竞争, 初始标志位 flag = false
 * 假如TaskA获取锁成功, 此时标志位 flag = false, 打印 '---'+i, 修改 flag = true, 唤醒等待线程, 释放锁
 * TaskA和TaskB再次进行锁竞争, 此时标志位 flag = true
 * 假如TaskA再次获取锁成功, 此时标志位 flag = true, 则TaskA线程等待, 并将线程让出(未释放锁)
 * TaskB获取锁成功, 此时标志位 flag = true, 打印 '***'+i, 修改 flag = false, 唤醒等待线程, 释放锁
 */
public class Test03 {

    /** 锁对象 */
    private static ReentrantLock lock = new ReentrantLock();

    /** condition 对象 */
    private static Condition condition = lock.newCondition();

    /** 标志位 */
    private static boolean flag = true;

    /** 任务 */
    static class TaskA implements Runnable {
        @Override
        public void run() {
            Stream.iterate(0, i -> i + 1).limit(50).forEach(o -> {
                lock.lock();
                try {
                    if (flag) {
                        System.out.println(Thread.currentThread().getName() + " >> " + "waiting...");
                        condition.await();
                    }
                    System.out.println(Thread.currentThread().getName() + " >> " + "---" + o);
                    flag = true;
                    condition.signal();
                } catch (Exception e) {
                    System.out.println("TaskA...线程异常" + e);
                } finally {
                    lock.unlock();
                }
            });
        }
    }

    /** 任务 */
    static class TaskB implements Runnable {
        @Override
        public void run() {
            Stream.iterate(0, i -> i + 1).limit(50).forEach(o -> {
                lock.lock();
                try {
                    if (! flag) {
                        System.out.println(Thread.currentThread().getName() + " >> " + "waiting...");
                        condition.await();
                    }
                    System.out.println(Thread.currentThread().getName() + " >> " + "***" + o);
                    flag = false;
                    condition.signal();
                } catch (Exception e) {
                    System.out.println("TaskB...线程异常" + e);
                } finally {
                    lock.unlock();
                }
            });
        }
    }

    public static void main(String[] args) throws InterruptedException {
        TaskA taskA = new TaskA();
        TaskB taskB = new TaskB();
        new Thread(taskA).start();
        new Thread(taskB).start();
    }
}

Test04

public class Test04 {

    /** 锁对象 */
    private static ReentrantLock lock = new ReentrantLock();

    /** condition 对象 */
    private static Condition conditionA = lock.newCondition();
    private static Condition conditionB = lock.newCondition();

    /** 标志位 */
    private static boolean flag = true;

    /** 任务 */
    static class TaskA implements Runnable {
        @Override
        public void run() {
            Stream.iterate(0, i -> i + 1).limit(50).forEach(o -> {
                lock.lock();
                try {
                    if (flag) {
                        System.out.println(Thread.currentThread().getName() + " >> " + "---" + o);
                        flag = false;
                        conditionB.signal();
                        conditionA.await();
                    }
                } catch (Exception e) {
                    System.out.println("TaskA...线程异常" + e);
                } finally {
                    lock.unlock();
                }
            });
        }
    }

    /** 任务 */
    static class TaskB implements Runnable {
        @Override
        public void run() {
            Stream.iterate(0, i -> i + 1).limit(50).forEach(o -> {
                lock.lock();
                try {
                    if (! flag) {
                        System.out.println(Thread.currentThread().getName() + " >> " + "***" + o);
                        flag = true;
                        conditionA.signal();
                        conditionB.await();
                    }
                } catch (Exception e) {
                    System.out.println("TaskB...线程异常" + e);
                } finally {
                    lock.unlock();
                }
            });
        }
    }

    public static void main(String[] args) throws InterruptedException {
        TaskA taskA = new TaskA();
        TaskB taskB = new TaskB();
        new Thread(taskA).start();
        new Thread(taskB).start();
    }
}

Test05

public class Test05 {

    /** 锁对象 */
    private static ReentrantLock lock = new ReentrantLock();

    /** condition 对象 */
    private static Condition conditionA = lock.newCondition();
    private static Condition conditionB = lock.newCondition();
    private static Condition conditionC = lock.newCondition();

    /** 标志位 */
    private static int flag = 1;

    /** 任务 */
    static class TaskA implements Runnable {
        @Override
        public void run() {
            Stream.iterate(0, i -> i + 1).limit(50).forEach(o -> {
                lock.lock();
                try {
                    if (flag == 1) {
                        System.out.println(Thread.currentThread().getName() + " >> " + "-" + flag);
                        flag = 2;
                        conditionB.signal();
                        conditionA.await();
                    }
                } catch (Exception e) {
                    System.out.println("TaskA...线程异常" + e);
                } finally {
                    lock.unlock();
                }
            });
        }
    }

    /** 任务 */
    static class TaskB implements Runnable {
        @Override
        public void run() {
            Stream.iterate(0, i -> i + 1).limit(50).forEach(o -> {
                lock.lock();
                try {
                    if (flag == 2) {
                        System.out.println(Thread.currentThread().getName() + " >> " + "--" + flag);
                        flag = 3;
                        conditionC.signal();
                        conditionB.await();
                    }
                } catch (Exception e) {
                    System.out.println("TaskB...线程异常" + e);
                } finally {
                    lock.unlock();
                }
            });
        }
    }

    /** 任务 */
    static class TaskC implements Runnable {
        @Override
        public void run() {
            Stream.iterate(0, i -> i + 1).limit(50).forEach(o -> {
                lock.lock();
                try {
                    if (flag == 3) {
                        System.out.println(Thread.currentThread().getName() + " >> " + "---" + flag);
                        flag = 1;
                        conditionA.signal();
                        conditionC.await();
                    }
                } catch (Exception e) {
                    System.out.println("TaskC...线程异常" + e);
                } finally {
                    lock.unlock();
                }
            });
        }
    }

    public static void main(String[] args) throws InterruptedException {
        TaskA taskA = new TaskA();
        TaskB taskB = new TaskB();
        TaskC taskC = new TaskC();
        new Thread(taskA).start();
        new Thread(taskB).start();
        new Thread(taskC).start();
    }
}