面试!线程题目求教!!!

题目:要求编写一个程序,启动三个线程,维护一个队列,

a,b线程向队列中添加随机数字,然后等待1秒内,

一直循环。c线程打印队列中新添加的数字。

注:对线程不太了解,今天遇到这个题目,有没有哪个老大哥出个解决方案!!!万分感谢!

    /**
     * 参考如下代码
     * @param args
     */
    public static void main(String[] args) {
        LinkedBlockingQueue<Integer> queue = new LinkedBlockingQueue<>(16);

// 启动线程a 添加数据
        new Thread(() -> {
            addElement(queue);
        }).start();

// 启动线程b 添加数据
        new Thread(() -> {
            addElement(queue);
        }).start();

// 启动线程c 消费数据
        new Thread(() -> {
            while (true) {
                try {
//                    队列为空,阻塞等待。
//                    队列不为空,从队首获取并移除一个元素,如果消费后还有元素在队列中,继续唤醒下一个消费线程进行元素移除。如果放之前队列是满元素的情况,移除完后要唤醒生产线程进行添加元素。
                    System.out.printf("%d ", queue.take());
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        }).start();
    }

/**
* 添加数据
*/
    private static void addElement(Queue<Integer> queue) {
        while (true) {
            queue.offer((int) (Math.random() * 100000));
            try {
                Thread.sleep(1000);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
    }