use of co.paralleluniverse.fibers.Fiber in project quasar by puniverse.
the class Pipeline method parallelTransfer.
private void parallelTransfer() throws SuspendExecution, InterruptedException {
// 1) Fire workers
for (int i = 0; i < parallelism; i++) {
strandFactory.newStrand(SuspendableUtils.runnableToCallable(new SuspendableRunnable() {
@Override
public void run() throws SuspendExecution, InterruptedException {
// Get first job
Pair<S, Channel<Channel<T>>> job = jobs.receive();
while (job != null) {
// Build result channel
final Channel<T> res = resultChannelBuilder.run();
// Process
transformer.call(job.getFirst(), res);
final Channel<Channel<T>> resWrapper = job.getSecond();
// Send result asynchronously
strandFactory.newStrand(SuspendableUtils.runnableToCallable(new SuspendableRunnable() {
@Override
public void run() throws SuspendExecution, InterruptedException {
resWrapper.send(res);
}
})).start();
// Get next job
job = jobs.receive();
}
// No more jobs, close results channel and quit worker
results.close();
}
})).start();
}
// 2) Send jobs asynchronously
strandFactory.newStrand(SuspendableUtils.runnableToCallable(new SuspendableRunnable() {
@Override
public void run() throws SuspendExecution, InterruptedException {
// Get first input
S s = from.receive();
while (s != null) {
final Channel<Channel<T>> resultWrapper = Channels.newChannel(1, Channels.OverflowPolicy.BLOCK, true, true);
jobs.send(new Pair<>(s, resultWrapper));
results.send(resultWrapper);
// Get next input
s = from.receive();
}
// No more inputs, close jobs channel and quit
jobs.close();
}
})).start();
// 3) Collect and transfer results asynchronously
try {
final Strand collector = strandFactory.newStrand(SuspendableUtils.runnableToCallable(new SuspendableRunnable() {
@Override
public void run() throws SuspendExecution, InterruptedException {
// Get first result
Channel<Channel<T>> resWrapper = results.receive();
while (resWrapper != null) {
// Get wrapper
Channel<T> res = resWrapper.receive();
// Get first actual result
T out = res.receive();
while (out != null) {
// Send to output channel
to.send(out);
// Increment counter
transferred.incrementAndGet();
// Get next result
out = res.receive();
}
resWrapper = results.receive();
}
// No more results, quit
}
})).start();
// TODO solve nasty instrumentation problems on Strand.join()
if (collector.isFiber()) {
Fiber f = (Fiber) collector.getUnderlying();
f.join();
} else
collector.join();
} catch (ExecutionException ee) {
throw new AssertionError(ee);
}
}
use of co.paralleluniverse.fibers.Fiber 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;
}
};
}
use of co.paralleluniverse.fibers.Fiber in project quasar by puniverse.
the class InitialSizeTest method testWithSize.
private void testWithSize(int stackSize) {
Fiber c = new Fiber(null, null, stackSize, this);
// assertEquals(getStackSize(c), stackSize);
boolean res = TestsHelper.exec(c);
assertEquals(res, false);
res = TestsHelper.exec(c);
assertEquals(res, true);
assertTrue(getStackSize(c) > 10);
}
use of co.paralleluniverse.fibers.Fiber in project quasar by puniverse.
the class InterfaceTest method testSuspend.
@Test
public void testSuspend() {
// final I i = new C();
Fiber co = new Fiber((String) null, null, new SuspendableRunnable() {
@Override
public final void run() throws SuspendExecution {
// next line causes an error because of incomplete merge in TypeInterpreter
// SomeInterface i = System.currentTimeMillis() > 0 ? new C() : new C2();
SomeInterface i = new C();
System.out.println("i = " + i);
i.doStuff();
}
});
while (!TestsHelper.exec(co)) System.out.println("State=" + co.getState());
System.out.println("State=" + co.getState());
}
use of co.paralleluniverse.fibers.Fiber in project quasar by puniverse.
the class LeakTest method leaky.
@Test
public void leaky() throws Exception {
Fiber co = new Fiber((String) null, null, this);
leaked = "leaked";
TestsHelper.exec(co);
TestsHelper.exec(co);
TestsHelper.exec(co);
Field stackField = Fiber.class.getDeclaredField("stack");
stackField.setAccessible(true);
Field objectsField = Stack.class.getDeclaredField("dataObject");
objectsField.setAccessible(true);
List<Object> stack = Arrays.asList((Object[]) objectsField.get(stackField.get(co)));
// System.out.println(stack);
assertThat(stack, not(hasItem(leaked)));
// assertThat(stack, everyItem(nullValue()));
// WeakReference<String> ref = new WeakReference<>(leaked);
// leaked = null;
// System.gc();
// System.gc();
// assertNull(ref.get());
}
Aggregations