public class Test { public static void main(String[] args) { BlockingQueue<Integer> queue = new LinkedBlockingQueue<Integer>(); java.lang.Runnable r = new MyRunnable(queue); Thread t = new Thread(r); t.start();
public class MyRunnable implements Runnable { public MyRunnable(BlockingQueue<Integer> queue) { this.queue = queue; } public void run() { Date d = new Date(); long starttime = d.getTime(); System.err.println(starttime); int count = 0; while(true) { try { Integer i = this.queue.poll(); if(i != null) { count ++; } if(count == 100000) { Date e = new Date(); long endtime = e.getTime(); System.err.println(count); System.err.println(endtime); System.err.print(endtime - starttime); break; }
public E poll() { final AtomicInteger count = this.count; if (count.get() == 0) return null; E x = null; int c = -1; final ReentrantLock takeLock = this.takeLock; takeLock.lock(); try { if (count.get() > 0) { x = extract(); c = count.getAndDecrement(); if (c > 1) notEmpty.signal(); } } finally { takeLock.unlock(); } if (c == capacity) signalNotFull(); return x; }
public boolean offer(E e) { if (e == null) throw new NullPointerException(); final AtomicInteger count = this.count; if (count.get() == capacity) return false; int c = -1; final ReentrantLock putLock = this.putLock; putLock.lock(); try { if (count.get() < capacity) { insert(e); c = count.getAndIncrement(); if (c + 1 < capacity) notFull.signal(); } } finally { putLock.unlock(); } if (c == 0) signalNotEmpty(); return c >= 0; }