/** * @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 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; }
public void start() { inputs = new AtomicReferenceArray<>(0); outputs = inputs; freeReceptors = new CopyOnWriteArrayList<>(); sequence = 0; finalSequence = 0; finalProduct = null; reuseReceptors = new ArrayList<>(0); executor = Executors.newSingleThreadExecutor(); started = true; if (poolSize > 0) { group = new ThreadGroup("FatPipe"); group.setMaxPriority(Thread.MAX_PRIORITY); } signals = new SignalImpl[ARBITARY_THREADS + poolSize]; threads = new Thread[ARBITARY_THREADS + poolSize]; startTime = System.currentTimeMillis(); sleptTime = 0; toSleepTime = 0; for (int i = 0; i < poolSize; i++) { Thread t = new Thread(group, this, name + "-" + i); t.setDaemon(true); threads[i] = t; t.start(); if (sleep == 0) t.setPriority(Thread.MAX_PRIORITY); else if (sleep < 0) t.setPriority(Thread.NORM_PRIORITY); else t.setPriority(Thread.MIN_PRIORITY); } for (int i = 0; i < signals.length; i++) { signals[i] = new SignalImpl(lock); } if (pool != null) pool.add(this); lock.release(); }