解决方案 »

  1.   

    虽然你没有贴代码出来,但是我建议你去掉 synchronised (queue)同时用synchronised (queue) { ... queue.take(); } 和 synchronized (queue) {... queue.put(..)},那是妥妥的死锁
      

  2.   

    当你take的时候,队列里没有值它就会锁死。
    public E take() throws InterruptedException {
            final ReentrantLock lock = this.lock;
            lock.lockInterruptibly();
            try {
                try {
                    while (count == 0)
                        notEmpty.await();
                } catch (InterruptedException ie) {
                    notEmpty.signal(); // propagate to non-interrupted thread
                    throw ie;
                }
                E x = extract();
                return x;
            } finally {
                lock.unlock();
            }
        }
    当你put的时候,队列里已经满它就会锁死。public void put(E e) throws InterruptedException {
            if (e == null) throw new NullPointerException();
            final E[] items = this.items;
            final ReentrantLock lock = this.lock;
            lock.lockInterruptibly();
            try {
                try {
                    while (count == items.length)
                        notFull.await();
                } catch (InterruptedException ie) {
                    notFull.signal(); // propagate to non-interrupted thread
                    throw ie;
                }
                insert(e);
            } finally {
                lock.unlock();
            }
        }
      

  3.   

    将put改成 poll(long timeout, TimeUnit unit)方法试试
      

  4.   

    notEmpty.await();
    notEmpty.signal()
      

  5.   

    把容量设成最大的
    BlockingQueue<String> blockQueue = new ArrayBlockingQueue<String>(Integer.MAX_VALUE );
      

  6.   

    5#说的很对,我也在JDK的源码中看到了这个。但我就想用一个队列,实现一个线程take一下,另外一个线程put一下,相当于是生产-消费者互相可以通信,代码中我没有用任何synchronized锁,难道用一个队列不行吗?我知道用两个队列可以实现
      

  7.   

    不需要你用任何synchronized,因为是堵塞队列
    当队列中没有数据的时候take会一直堵塞,直到有线程往队列中put,
    当队列已经满了的时候put会一直堵塞,直到有线程从队列中take.
    只能介绍这么多的原理了。希望能帮助到我,这是我写socket,多线程编程的一点经验。
      

  8.   

    还有一种方式就是自己实现堵塞队伍,
    当队列中没有值是wait(),当队列中有值是再,调用 notify.或使用lock锁。
      

  9.   

    1.在A库创建B库dblink。
    2.在A库中可以直接引用B库的表进行查询操作。
    3.再根据查询做相关的插入。具体dblink的创建请百度