实现一个容器,提供两个方法,add,size;写两个线程,线程1添加10个元素到容器中,线程2实现监控元素的个数,当个数到5个时,线程2给出提示并结束

多线程面试题

实现一个容器,提供两个方法,add,size;写两个线程,线程1添加10个元素到容器中,线程2实现监控元素的个数,当个数到5个时,线程2给出提示并结束

1.wait和notify实现

 private static  List<String> list = new ArrayList<>();

    public static void main(String[] args) {
        Object locak = new Object();
        Thread thread1 = new Thread(new Runnable() {
            @Override
            public void run() {
                synchronized (locak){
                    if(list.size()!=5){
                        try {
                            locak.wait();
                        } catch (InterruptedException e) {
                            e.printStackTrace();
                        }
                    }
                    System.out.println("监测到容器长度为5,线程2结束...");
                    locak.notify();//唤醒其他等锁对象
                }

            }
        });

        Thread thread = new Thread(new Runnable() {
            @Override
            public void run() {
                synchronized (locak){
                    for (int i = 0; i <10 ; i++) {
                        System.out.println("添加对象" + i);
                        list.add(String.valueOf(i));
                        if(list.size()==5){
                            locak.notify();
                            try {
                                locak.wait();
                            } catch (InterruptedException e) {
                                e.printStackTrace();
                            }

                        }
                    }
                    System.out.println("线程1结束...");
                }

            }
        });



        thread.start();
        thread1.start();
    }

2.LockSupport实现

private static List<String> list = new ArrayList<>();
    static Thread t1 =null,t2 = null;

    public static void main(String[] args) {
        t1 = new Thread(new Runnable() {
            @Override
            public void run() {
                for (int i = 0; i <10 ; i++) {
                    System.out.println("添加对象" + i);
                    list.add(String.valueOf(i));
                    if(list.size()==5){
                        LockSupport.unpark(t2);
                        LockSupport.park();
                    }
                }
                System.out.println("线程1结束...");
            }
        });

        t2 = new Thread(new Runnable() {
            @Override
            public void run() {
               LockSupport.park();
                System.out.println("t2 结束");
                LockSupport.unpark(t1);
                System.out.println("线程2结束。。。。");
            }

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

3.Semaphore实现

public static void main(String[] args) {
        Semaphore semaphoreA = new Semaphore(0);
        Semaphore semaphoreB = new Semaphore(0);
        Container container = new Container();
        ThreadA threadA = new ThreadA(container,semaphoreA,semaphoreB);
        Thread thread = new Thread(threadA);
        ThreadB threadB = new ThreadB(semaphoreA,semaphoreB);
        Thread thread1 = new Thread(threadB);
        thread.start();
        thread1.start();

    }

   static class Container {

        private List<Integer> list = new ArrayList<>();

        public void add(int a){
            list.add(a);
        }
        public int size(){
            return list.size();
        }
    }

    static class ThreadA implements Runnable {

        private Container container;

        private Semaphore semaphoreA;

        private Semaphore semaphoreB;

        public ThreadA(Container container, Semaphore semaphoreA, Semaphore semaphoreB) {
            this.container = container;
            this.semaphoreA = semaphoreA;
            this.semaphoreB = semaphoreB;
        }

        @Override
        public void run() {
            for (int i = 0; i <10 ; i++) {
                if(container.size()==5){
                    semaphoreB.release();
                    try {
                        semaphoreA.acquire();
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                }
                System.out.println("添加数据是"+i);
                container.add(i);
            }
        }
    }

   static class ThreadB implements Runnable{

        private Semaphore semaphoreA;

        private Semaphore semaphoreB;

        public ThreadB(Semaphore semaphoreA, Semaphore semaphoreB) {

            this.semaphoreA = semaphoreA;
            this.semaphoreB = semaphoreB;
        }

        @Override
        public void run() {
            try {
                semaphoreB.acquire();
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            System.out.println("线程b结束");
            semaphoreA.release();
        }
    }

4.CuntDownLatch实现

 public static void main(String[] args) {
        MyContainer myContainer = new MyContainer();
        CountDownLatch countDownLatch = new CountDownLatch(1);
        CountDownLatch countDownLatch1 = new CountDownLatch(1);
        ThreadA threadA = new ThreadA(countDownLatch,countDownLatch1,myContainer);
        ThreadB threadB = new ThreadB(countDownLatch,countDownLatch1);
        Thread thread = new Thread(threadA);
        Thread thread1 = new Thread(threadB);
        thread.start();
        thread1.start();
    }
    static class MyContainer{
        List<Integer> list = new ArrayList<>();
        public void add(int a){
            list.add(a);
        }
        public int size(){
            return list.size();
        }
    }

    static class ThreadA implements Runnable{
        CountDownLatch countDownLatchA;
        CountDownLatch countDownLatchB;
        MyContainer myContainer;

        public ThreadA(CountDownLatch countDownLatchA, CountDownLatch countDownLatchB, MyContainer myContainer) {
            this.countDownLatchA = countDownLatchA;
            this.countDownLatchB = countDownLatchB;
            this.myContainer = myContainer;
        }

        @Override
        public void run() {
            for (int i = 0; i <10 ; i++) {
                if(myContainer.size()==5){
                    try {
                        countDownLatchB.countDown();
                        countDownLatchA.await();

                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                }
                System.out.println("添加数据是"+i);
                myContainer.add(i);
            }
            System.out.println("线程a执行完毕");
        }
    }
    static class ThreadB implements Runnable{

        CountDownLatch countDownLatchA;
        CountDownLatch countDownLatchB;

        public ThreadB(CountDownLatch countDownLatchA, CountDownLatch countDownLatchB) {
            this.countDownLatchA = countDownLatchA;
            this.countDownLatchB = countDownLatchB;
        }

        @Override
        public void run() {

            try {
                countDownLatchB.await();
                System.out.println("线程b执行完毕");
                countDownLatchA.countDown();
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
    }

5.CyclicBarrier实现

 public static void main(String[] args) {
        ThreadLatch.MyContainer myContainer = new ThreadLatch.MyContainer();
        CyclicBarrier cyclicBarrier = new CyclicBarrier(1, new Runnable() {
            @Override
            public void run() {
                System.out.println("线程b执行完成");
            }
        });
        ThreadA  threadA= new ThreadA(cyclicBarrier,myContainer);
        Thread thread = new Thread(threadA);
        thread.start();
    }
    static class MyContainer{
        List<Integer> list = new ArrayList<>();
        public void add(int a){
            list.add(a);
        }
        public int size(){
            return list.size();
        }
    }

    static class ThreadA implements Runnable{
        CyclicBarrier cyclicBarrierA;
        ThreadLatch.MyContainer myContainer;

        public ThreadA(CyclicBarrier cyclicBarrierA,  ThreadLatch.MyContainer myContainer) {
            this.cyclicBarrierA = cyclicBarrierA;
            this.myContainer = myContainer;
        }

        @Override
        public void run() {
            for (int i = 0; i <10 ; i++) {
                if(myContainer.size()==5){
                    try {
                        cyclicBarrierA.await();
                    } catch (Exception e) {
                        e.printStackTrace();
                    }
                }
                System.out.println("添加数据是"+i);
                myContainer.add(i);
            }
            System.out.println("线程a执行完毕");
        }
    }

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

stayhungerstayflush

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

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

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

打赏作者

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

抵扣说明:

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

余额充值