/** * Performs flush. * * @throws GridException If failed. */ private void doFlush() throws GridException { lastFlushTime = U.currentTimeMillis(); List<GridFuture> activeFuts0 = null; int doneCnt = 0; for (GridFuture<?> f : activeFuts) { if (!f.isDone()) { if (activeFuts0 == null) activeFuts0 = new ArrayList<>((int) (activeFuts.size() * 1.2)); activeFuts0.add(f); } else { f.get(); doneCnt++; } } if (activeFuts0 == null || activeFuts0.isEmpty()) return; while (true) { Queue<GridFuture<?>> q = null; for (Buffer buf : bufMappings.values()) { GridFuture<?> flushFut = buf.flush(); if (flushFut != null) { if (q == null) q = new ArrayDeque<>(bufMappings.size() * 2); q.add(flushFut); } } if (q != null) { assert !q.isEmpty(); boolean err = false; for (GridFuture fut = q.poll(); fut != null; fut = q.poll()) { try { fut.get(); } catch (GridException e) { if (log.isDebugEnabled()) log.debug("Failed to flush buffer: " + e); err = true; } } if (err) // Remaps needed - flush buffers. continue; } doneCnt = 0; for (int i = 0; i < activeFuts0.size(); i++) { GridFuture f = activeFuts0.get(i); if (f == null) doneCnt++; else if (f.isDone()) { f.get(); doneCnt++; activeFuts0.set(i, null); } else break; } if (doneCnt == activeFuts0.size()) return; } }
/** * @param time Test time. * @param writers Number of writers threads. * @throws Exception If failed. */ public void testQueue(long time, int writers) throws Exception { System.out.println("Start test [writers=" + writers + ", time=" + time + "]"); Thread rdr = new Thread() { @SuppressWarnings({"InfiniteLoopStatement", "unchecked"}) @Override public void run() { try { while (true) { Future fut; while ((fut = queue.poll()) != null) { qSize.decrementAndGet(); fut.onDone(true); } long size = qSize.get(); if (size == 0) { synchronized (mux) { while (queue.isEmpty()) { mux.wait(); } } } } } catch (InterruptedException ignore) { } } }; rdr.start(); List<Thread> putThreads = new ArrayList<>(); for (int i = 0; i < writers; i++) { putThreads.add( new Thread() { @SuppressWarnings("CallToNotifyInsteadOfNotifyAll") @Override public void run() { try { while (!stop) { long id = cnt.incrementAndGet(); Future<Boolean> fut = new Future<Boolean>(new Message(id)); queue.offer(fut); long size = qSize.incrementAndGet(); if (size == 1) { synchronized (mux) { mux.notify(); } } Boolean res = fut.get(); if (!res) { System.out.println("Error"); } } } catch (Exception e) { e.printStackTrace(); } } }); } for (Thread t : putThreads) t.start(); Thread.sleep(time); stop = true; for (Thread t : putThreads) t.join(); rdr.interrupt(); rdr.join(); System.out.println("Total: " + cnt.get()); System.gc(); System.gc(); System.gc(); }