【面试】--三个线程轮流打印ABC

54 篇文章 0 订阅
37 篇文章 0 订阅

0 synchronized 实现

public class SynchronizeABC extends Thread {

    private String name;
    private Object pre;
    private Object self;


    public SynchronizeABC(String name, Object pre, Object self) {
        this.name = name;
        this.pre = pre;
        this.self = self;
    }

    @Override
    public void run() {
        for (; ; ) {
            synchronized (pre) {
                synchronized (self) {
                    System.out.println(name);
                    try {
                        Thread.sleep(500);
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                    self.notifyAll();
                }
                try {
                    pre.wait();
                    System.out.println("wait "+name);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }

        }
    }

    public static void main(String[] args) throws InterruptedException {
        Object a = new Object();
        Object b = new Object();
        Object c = new Object();

        new SynchronizeABC("A", c, a).start();
        Thread.sleep(500);
        new SynchronizeABC("B", a, b).start();
        Thread.sleep(500);
        new SynchronizeABC("C", b, c).start();

        Thread.currentThread().join();


    }
}

1 lock实现

public class ABCLock {

    private static Lock lock = new ReentrantLock();
    private static Condition conditionA = lock.newCondition();
    private static Condition conditionB = lock.newCondition();
    private static Condition conditionC = lock.newCondition();


    private static int count = 1;

    private static Thread threadA = new Thread(new Runnable() {
        @Override
        public void run() {

            while (true) {
                try {
                    lock.lock();

                    while (count % 3 == 1) {

                        System.out.print("A");
                        Thread.sleep(500);
                        count++;

                    }

                } catch (Exception e) {

                } finally {
                    lock.unlock();
                }

            }
        }
    });

    private static Thread threadB = new Thread(new Runnable() {
        @Override
        public void run() {
            while (true) {

                try {
                    lock.lock();

                    while (count % 3 == 2) {

                        System.out.print("B");
                        Thread.sleep(500);
                        count++;

                    }

                } catch (Exception e) {

                } finally {
                    lock.unlock();
                }
            }
        }

    });
    private static Thread threadC = new Thread(new Runnable() {
        @Override
        public void run() {
            while (true) {

                try {
                    lock.lock();
                    while (count % 3 == 0) {

                        System.out.print("C");
                        Thread.sleep(500);
                        count++;

                    }

                } catch (Exception e) {

                } finally {
                    lock.unlock();
                }

            }
        }
    });

    public static void main(String[] args) throws InterruptedException {
        threadA.start();
        threadB.start();
        threadC.start();
        Thread.currentThread().join();
    }
}

2 Lock + Condition

public class ABCLock {

    private static Lock lock = new ReentrantLock();
    private static Condition conditionA = lock.newCondition();
    private static Condition conditionB = lock.newCondition();
    private static Condition conditionC = lock.newCondition();


    private static int count = 1;

    private static Thread threadA = new Thread(new Runnable() {
        @Override
        public void run() {

            while (true) {
                try {
                    lock.lock();

                    while (count % 3 != 1)
                        conditionA.wait();

                    System.out.print("A");
                    Thread.sleep(500);
                    count++;
                    conditionB.signal();


                } catch (Exception e) {

                } finally {
                    lock.unlock();
                }

            }
        }
    });

    private static Thread threadB = new Thread(new Runnable() {
        @Override
        public void run() {
            while (true) {

                try {
                    lock.lock();

                    while (count % 3 != 2)
                        conditionB.wait();

                    System.out.print("B");
                    Thread.sleep(500);
                    count++;
                    conditionC.signal();


                } catch (Exception e) {

                } finally {
                    lock.unlock();
                }
            }
        }

    });
    private static Thread threadC = new Thread(new Runnable() {
        @Override
        public void run() {
            while (true) {

                try {
                    lock.lock();
                    while (count % 3 != 0)
                        conditionC.wait();

                    System.out.print("C");
                    Thread.sleep(500);
                    count++;
                    conditionA.signal();


                } catch (Exception e) {

                } finally {
                    lock.unlock();
                }

            }
        }
    });

    public static void main(String[] args) throws InterruptedException {
        threadA.start();
        threadB.start();
        threadC.start();
        Thread.currentThread().join();
    }
}


3 信号量

public class ABCSemaphore {
    private static Semaphore semaphoreA = new Semaphore(1);
    private static Semaphore semaphoreB = new Semaphore(0);
    private static Semaphore semaphoreC = new Semaphore(0);

    private static Thread threadA = new Thread(new Runnable() {
        @Override
        public void run() {

            while (true) {
                try {
                    semaphoreA.acquire();

                    System.out.print("A");
                    Thread.sleep(500);
                    semaphoreB.release();


                } catch (Exception e) {

                } finally {

                }

            }
        }
    });

    private static Thread threadB = new Thread(new Runnable() {
        @Override
        public void run() {
            while (true) {
                try {
                    semaphoreB.acquire();

                    System.out.print("B");
                    Thread.sleep(500);
                    semaphoreC.release();


                } catch (Exception e) {

                } finally {

                }

            }
        }

    });
    private static Thread threadC = new Thread(new Runnable() {
        @Override
        public void run() {
            while (true) {
                try {
                    semaphoreC.acquire();

                    System.out.print("C");
                    Thread.sleep(500);
                    semaphoreA.release();


                } catch (Exception e) {

                } finally {

                }

            }

        }

    });

    public static void main(String[] args) throws InterruptedException {
        threadA.start();
        threadB.start();
        threadC.start();
        Thread.currentThread().join();
    }

}

  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

自驱

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值