Search in sources :

Example 1 with FiberScheduler

use of co.paralleluniverse.fibers.FiberScheduler in project quasar by puniverse.

the class StrandFactoryBuilder method build.

/**
 * Creates and returns the {@link StrandFactory} that will create new strands based on this builder's settings.
 *
 * @return a new {@link StrandFactory}.
 */
public StrandFactory build() {
    if (fiber == null)
        throw new IllegalStateException("setFiber or setThread must be called before calling build");
    final boolean _fiber = fiber;
    final boolean _daemon = daemon;
    final FiberScheduler _fs = fs;
    final String _nameFormat = nameFormat;
    final Strand.UncaughtExceptionHandler _ueh = ueh;
    final int _stackSize = stackSize != null ? stackSize : 0;
    final Integer _priority = priority;
    final AtomicLong _count = (nameFormat != null) ? new AtomicLong(0) : null;
    return new StrandFactory() {

        @Override
        public Strand newStrand(SuspendableCallable<?> target) {
            final String name = _nameFormat != null ? String.format(_nameFormat, _count.getAndIncrement()) : null;
            final Strand s;
            if (_fiber) {
                s = _fs != null ? new Fiber(name, _fs, _stackSize, target) : new Fiber(name, _stackSize, target);
            } else {
                final Thread t = new Thread(null, Strand.toRunnable(target), name != null ? name : "Thread-" + nextThreadNum(), _stackSize);
                if (name != null)
                    t.setName(name);
                t.setDaemon(_daemon);
                if (_priority != null)
                    t.setPriority(_priority);
                s = Strand.of(t);
            }
            if (_ueh != null)
                s.setUncaughtExceptionHandler(_ueh);
            return s;
        }
    };
}
Also used : Fiber(co.paralleluniverse.fibers.Fiber) AtomicLong(java.util.concurrent.atomic.AtomicLong) FiberScheduler(co.paralleluniverse.fibers.FiberScheduler)

Aggregations

Fiber (co.paralleluniverse.fibers.Fiber)1 FiberScheduler (co.paralleluniverse.fibers.FiberScheduler)1 AtomicLong (java.util.concurrent.atomic.AtomicLong)1