Android面试-Java基础

Java垃圾回收机制

Java中程序员不需要显示去释放一个对象的内存,由虚拟机自行执行。JVM有一个垃圾回收线程,它是低优先级的,只有的虚拟机空闲或当前堆内存不足时才会出发执行,扫描没有被引用的对象,并添加到要回收的集合中,进行回收。

Java内存模型

Java多线程并发和线程池

Java实现阻塞队列

方式一:

public class BlockingQueue {
    //基于链表的list
    private List queue = new LinkedList();
    //元素个数的限制
    private int limit = 10;
    public BlockingQueue(int limit) {
        this.limit = limit;
    }

    //添加元素
    public synchronized void enqueue(Object item) throws InterruptedException {
        while(this.queue.size() == this.limit) {
            wait();
        }
        if(this.queue.size() == 0) {
            notifyAll();
        }
        this.queue.add(item);
    }

    //删除元素
    public synchronized Object dequeue() throws InterruptedException {
        while(this.queue.size() == 0) {
            wait();
        }
        if(this.queue.size() == this.limit) {
            notifyAll();
        }
        return this.queue.remove(0);
    }
}
原文链接:https://blog.csdn.net/change_on/article/details/79808806

方式二:

/**
*用可重入锁写 阻塞队列
*/
public class BlockQueue {
    private int size;
    private List queue;

    private Lock lock = new ReentrantLock();
    private Condition full = lock.newCondition();
    private Condition empty = lock.newCondition();

    public BlockQueue() {
        this(3);
    }

    public BlockQueue(int size) {
        this.size = size;
        queue = new LinkedList();
    }

    public void push(T element) throws InterruptedException {
        lock.lock();
        try {
            while (queue.size() == size) {
                System.out.println("队列满。。。");
                full.await();
            }
            queue.add(element);
            empty.signal();
        } finally {
            lock.unlock();
        }
    }

    public T pop() throws InterruptedException {
        lock.lock();
        try {
            while (queue.size() == 0) {
                System.out.println("队列空。。。");
                empty.await();
            }
            full.signal();
            return (T) queue.remove(0);
        } finally {
            lock.unlock();
        }
    }

    public static void main(String[] args) throws InterruptedException {
        final BlockQueue blockQueue = new BlockQueue(3);
        blockQueue.push(1);
        blockQueue.push(1);
//        blockQueue.push(1);
//        blockQueue.push(1);

        new Thread(new Runnable() {
            @Override
            public void run() {
                try {
                    blockQueue.pop();
                    blockQueue.pop();
                    blockQueue.pop();
//                    blockQueue.pop();
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        }).start();
//        System.out.println(blockQueue.pop());
//        blockQueue.push(2);
//        System.out.println(blockQueue.pop());
//        blockQueue.push(3);
//        System.out.println(blockQueue.pop());
//
//        blockQueue.push(5);
//        blockQueue.push(5);
//        System.out.println(blockQueue.pop());
    }
}

你可能感兴趣的:(java,多线程)