/** * @return if <0, then this pipe should be stopped. If ==0, it should not wait. If >0, if it * should wait */ int execute() { if (!lock.tryAcquire()) return 1; // currently being accessed Thread myThread = Thread.currentThread(); int threadIndex = -1; for (int i = 0; i < threads.length; i++) { if (threads[i] == null) { threads[i] = myThread; threadIndex = i; break; } if (myThread != threads[i]) continue; threadIndex = i; break; } Signal signal; if (threadIndex < 0) { signal = dummySignal; } else { SignalImpl s = signals[threadIndex]; s.threadIndex = threadIndex; s.signaled = false; signal = s; } boolean hasData; try { hasData = poll(signal, null); } finally { signal.signal(); if (threadIndex < 0) lock.release(); } return 0; }
@Override @SuppressWarnings({"unchecked"}) public void shutdown(long time, TimeUnit unit) { started = false; try { lock.tryAcquire(time, unit); } catch (InterruptedException e) { logger.warn("shutdown", e); } if (group != null) group.interrupt(); consumers = new Consumer[0]; consumerSeqs = new long[0]; outUse = new AtomicLongArray(0); executor.shutdown(); }
@Override public boolean setConsumers(List<Consumer<S>> list, long time, TimeUnit unit) throws IllegalArgumentException { if (list == null) throw new IllegalArgumentException("consumer list is not set"); for (Consumer<S> consumer : list) { if (consumer == null) throw new IllegalArgumentException("consumer is null"); } try { if (!lock.tryAcquire(time, unit)) throw new IllegalArgumentException("can't lock producer"); } catch (InterruptedException e) { return false; } try { return setConsumers(list); } catch (InterruptedException e) { logger.warn("setting consumer", e); } finally { lock.release(); } return false; }