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();
    }
}

你可能感兴趣的:(面试)