04 面试题系列 | 编程题:实现一个容器,提供两个方法,add,size。写两个线程,线程 1 添加 10 个元素到容器中,线程 2 实现监控元素的个数,当个数到 5 个时,线程 2 给出提示并结束

题目

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

  1. 用普通线程方法来实现
  2. 用 volitile 关键字实现
  3. 用 wait 和 notify 实现
  4. 使用 latch 替代 wait notify 实现
用普通线程方法来实现
public class MyContainer {

    List list = new ArrayList();

    public void add(Object o) {
        list.add(o);
    }

    public int size() {
        return list.size();
    }

    public static void main(String[] args) {
        MyContainer container = new MyContainer();
        new Thread(() -> {
            System.out.println("Thread-1 start");
            for(int i=0; i<10; i++) {
                container.add(new Object());
                System.out.println("add " + i);

                try {
                    TimeUnit.SECONDS.sleep(1);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }

            }
            System.out.println("Thread-1 end");
        },"Thread-1 start").start();

        new Thread(() -> {
            System.out.println("Thread-2 start");
            while(true) {
                if(container.size() == 5) {
                    break;
                }
            }
            System.out.println("Thread-2 end");
        },"Thread-2").start();
    }

}
用 volitile 关键字实现
public class MyContainer {

    volatile List list = new ArrayList();

    public void add(Object o) {
        list.add(o);
    }

    public int size() {
        return list.size();
    }

     public static void main(String[] args) {
        MyContainer container = new MyContainer();
        new Thread(() -> {
            System.out.println("Thread-1 start");
            for(int i=0; i<10; i++) {
                container.add(new Object());
                System.out.println("add " + i);

                try {
                    TimeUnit.SECONDS.sleep(1);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }

            }
            System.out.println("Thread-1 end");
        },"Thread-1 start").start();

        new Thread(() -> {
            System.out.println("Thread-2 start");
            while(true) {
                if(container.size() == 5) {
                    break;
                }
            }
            System.out.println("Thread-2 end");
        },"Thread-2").start();
    }

}
用 wait 和 notify 实现
public class MyContainer {

    /**
     * 添加 volatile,使 t2 能够得到通知
     */
    volatile List list = new ArrayList();

    public void add(Object object) {
        list.add(object);
    }

    public int size() {
        return list.size();
    }

    public static void main(String[] args) {
        MyContainer container = new MyContainer();
        Object lock = new Object();

        // 线程一
        new Thread(() -> {
            System.out.println("Thread-2 start");
            if (container.size() != 5) {
                try {
                    // 在调用 wait() 或者 notify() 之前,必须使用 synchronized 语义绑定住被 wait/notify 的对象
                    // 否则会产生 java.lang.IllegalMonitorStateException
                    synchronized (lock) {
                        lock.wait();
                    }
                } catch (Exception e) {
                    e.printStackTrace();
                    System.out.println("list.size() == 5, Thread-2 end");
                }
            }
        }, "Thread-2").start();

        //线程二
        new Thread(() -> {
            System.out.println("Thread-1 start");
            for (int i = 0; i < 10; i++) {
                container.add(new Object());
                System.out.println("add " + i);

                if (container.size() == 5) {
                    synchronized (lock) {
                        lock.notify();
                    }
                }

                try {
                    TimeUnit.SECONDS.sleep(1);
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        }, "Thread-1").start();
    }
}
使用 latch 替代 wait notify 实现
public class MyContainer {

    /**
     * 添加 volatile,使 t2 能够得到通知
     */
    volatile List list = new ArrayList();

    public void add(Object object) {
        list.add(object);
    }

    public int size() {
        return list.size();
    }

    public static void main(String[] args) {
        MyContainer2 container = new MyContainer2();
        CountDownLatch latch = new CountDownLatch(1);
        //线程一
        new Thread(() -> {
            System.out.println("Thread-2 start");
            if (container.size() != 5) {
                try {
                    latch.await();
                } catch (Exception e) {
                    e.printStackTrace();
                }
                System.out.println("list.size() == 5, Thread-2 end");
            }
        }, "Thread-2").start();
        //线程二
        new Thread(() -> {
            System.out.println("Thread-1 start");
            for (int i = 0; i < 10; i++) {
                container.add(new Object());
                System.out.println("add " + i);
                if (container.size() == 5) {
                    latch.countDown();
                }
                try {
                    TimeUnit.SECONDS.sleep(1);
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        }, "Thread-1").start();
    }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值