多线程打印AB、ABC

package com.example.demo.test;


import org.junit.Test;

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


public class TestABC {

    @Test
    public void t1() {
        CountDownLatch count = new CountDownLatch(2);

        new Thread(new Runnable() {
            @Override
            public void run() {
                System.out.println("线程01开始");
                count.countDown();
                System.out.println("线程01-1");

            }
        }).start();

        new Thread(new Runnable() {
            @Override
            public void run() {
                System.out.println("线程02开始");
                count.countDown();
                System.out.println("线程02-1");

            }
        }).start();

        try {
            System.out.println("主线程等待");
            count.await();
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        if (count.getCount() == 0) {
            System.out.println("主线程工作");
        }
    }

    /**
     * 多线程循环打印AB
     * sync + wait + notify
     */
    final static Object lock = new Object();

    @Test
    public void t2() {
        CountDownLatch countDownLatch = new CountDownLatch(10);
        Thread t1, t2 = null;


        t1 = new Thread(new Runnable() {
            @Override
            public void run() {
                while (countDownLatch.getCount() != 0) {
                    synchronized (lock) {
                        countDownLatch.countDown();
                        System.out.println("Thread-1 - A");
                        try {
                            lock.notify();
                            lock.wait();
                        } catch (InterruptedException e) {
                            e.printStackTrace();
                        }
                    }

                }
            }

        });

        t2 = new Thread(new Runnable() {
            @Override
            public void run() {
                while (countDownLatch.getCount() != 0) {
                    synchronized (lock) {
                        countDownLatch.countDown();
                        System.out.println("Thread-2 - B");
                        try {
                            lock.notify();
                            lock.wait();
                        } catch (InterruptedException e) {
                            e.printStackTrace();
                        }
                    }
                }
            }
        });
        t1.start();
        t2.start();
        try {
            countDownLatch.await();
            System.out.println(countDownLatch.getCount());
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }


    static Thread t1, t2, t3;
    static Lock lock1 = new ReentrantLock();
    static Condition conditionA = lock1.newCondition();
    static Condition conditionB = lock1.newCondition();
    static Condition conditionC = lock1.newCondition();

    @Test
    public void t4() {


        t1 = new Thread(new Runnable() {
            @Override
            public void run() {
                for (int i = 0; i < 10; i++) {
                    lock1.lock();
                    System.out.print("A ");
                    conditionB.signalAll();
                    try {
                        conditionA.await();
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                    lock1.unlock();
                }
            }
        });
        t2 = new Thread(new Runnable() {
            @Override
            public void run() {
                for (int i = 0; i < 10; i++) {
                    lock1.lock();
                    try {
                        conditionB.await();
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                    System.out.print("B ");
                    conditionC.signalAll();
                    lock1.unlock();
                }
            }
        });
        t3 = new Thread(new Runnable() {
            @Override
            public void run() {
                for (int i = 0; i < 10; i++) {
                    lock1.lock();
                    try {
                        conditionC.await();
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                    System.out.println("C");
                    conditionA.signalAll();
                    lock1.unlock();
                }
            }
        });
        t3.start();
        t2.start();
        t1.start();
    }

    @Test
    public void t5() {

        t1 = new Thread(new Runnable() {
            @Override
            public void run() {
                while (true) {
                    System.out.print("A");
                    LockSupport.unpark(t2);
                    LockSupport.park();

                }
            }
        });

        t2 = new Thread(new Runnable() {
            @Override
            public void run() {
                while (true) {
                    LockSupport.park();
                    System.out.println("B");
                    LockSupport.unpark(t1);
                }
            }
        });

        t1.start();
        t2.start();


    }


    public static void main(String[] args) {
        t1 = new Thread(new Runnable() {
            @Override
            public void run() {
                for (int i = 0; i < 10; i++) {
                    System.out.print("A ");
                    LockSupport.unpark(t2);
                    LockSupport.park(t1);
                }
            }
        });
        t2 = new Thread(new Runnable() {
            @Override
            public void run() {
                for (int i = 0; i < 10; i++) {
                    LockSupport.park();
                    System.out.print("B ");
                    LockSupport.unpark(t3);
                }
            }
        });
        t3 = new Thread(new Runnable() {
            @Override
            public void run() {
                for (int i = 0; i < 10; i++) {
                    LockSupport.park();
                    System.out.println("C");
                    LockSupport.unpark(t1);
                }
            }
        });
        t3.start();
        t2.start();
        t1.start();
    }


}

多线程打印ABABC

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值