本文整理汇总了Java中co.paralleluniverse.fibers.Fiber类的典型用法代码示例。如果您正苦于以下问题:Java Fiber类的具体用法?Java Fiber怎么用?Java Fiber使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。
Fiber类属于co.paralleluniverse.fibers包,在下文中一共展示了Fiber类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: submit
import co.paralleluniverse.fibers.Fiber; //导入依赖的package包/类
@Suspendable
public Future<T> submit(Callable<T> callable) {
return new Fiber<T>() {
@Override
protected T run() throws SuspendExecution, InterruptedException {
try {
return callable.call();
} catch (Exception e) {
e.printStackTrace();
} finally {
channel.send(this);
}
return null;
}
}.start();
}
示例2: main
import co.paralleluniverse.fibers.Fiber; //导入依赖的package包/类
/**
* In current state - no where near as good performance as vertx, but syntax is better
*/
public static void main(String[] args) throws Exception {
int count = 9999;
CountDownLatch done = new CountDownLatch(count);
Stopwatch stopwatch = Stopwatch.createStarted();
IntStream.range(0,count).forEach(i ->
new Fiber((SuspendableRunnable) () -> {
try (CloseableHttpResponse response = client.execute(new HttpGet(url))){
response.getStatusLine().getStatusCode();
} catch (Exception e) {
e.printStackTrace();
} finally {
done.countDown();
}
}).start()
);
done.await(15, TimeUnit.SECONDS);
TaskRunner.print(
new TaskRunner.Result(stopwatch.elapsed(TimeUnit.MILLISECONDS), Runtime.getRuntime().totalMemory()),
done.getCount(),
FiberHttpRequest.class.getSimpleName());
}
示例3: record
import co.paralleluniverse.fibers.Fiber; //导入依赖的package包/类
/**
* Record events in a separate Fiber using one or more Recorders.
*
* This method returns immediately after starting the Fiber for recording events.
*
* @param fiberName the name of the fiber that records events.
* @param fe an optional scheduler for the spawned recording fiber, null to use the default.
* @param rp a channel on which to receive TimingEvents, which are passed to the underlying
* Recorders.
* @param rs zero or more Recorders, each of which receives every event (after the delay period)
* in the order they are passed to this method.
* @param <T> the type of the Response object in each TimingEvent.
*/
@SafeVarargs
static <T> Fiber<Void> record(final String fiberName,
final FiberScheduler fe,
final ReceivePort<TimingEvent<T>> rp,
final Recorder<T>... rs)
{
return new Fiber<Void>(fiberName, fe != null ? fe : DefaultFiberScheduler.getInstance(), () -> {
while (true) {
final TimingEvent<T> event = rp.receive();
if (event == null) {
break;
}
for (final Recorder<T> r : rs) {
r.record(event);
}
}
return null;
}).start();
}
示例4: main
import co.paralleluniverse.fibers.Fiber; //导入依赖的package包/类
public static void main(String[] args) throws SuspendExecution, InterruptedException {
final IntervalGenerator intervalGen = new ConstantIntervalGenerator(10000000);
final RequestExecutor<EchoRequest, EchoResponse> requestExector = new EchoRequestExecutor();
final Channel<EchoRequest> requestCh = Channels.newChannel(-1);
final Channel<TimingEvent<EchoResponse>> eventCh = Channels.newChannel(-1);
// Requests generator
new Fiber<Void>("req-gen", () -> {
for (int i=0; i < 1000; ++i) {
final EchoRequest req = new EchoRequest();
req.setMessage("foo");
requestCh.send(req);
}
requestCh.close();
}).start();
final Histogram histogram = new Histogram(3600000000L, 3);
// Event recording, both HistHDR and logging
record(eventCh, new HdrHistogramRecorder(histogram, 1000000), new LoggingRecorder(LOG));
JBender.loadTestThroughput(intervalGen, 0, requestCh, requestExector, eventCh);
histogram.outputPercentileDistribution(System.out, 1000.0);
}
示例5: spawn
import co.paralleluniverse.fibers.Fiber; //导入依赖的package包/类
/**
* 产生一个纤程actor,并且使用指定的Handler来放入指定的调度器执行
* @param fibSche 调度器
* @param ah 可运行单元
* @return 新Actor的Id
*/
@Suspendable
public ActorId spawn(FiberScheduler fibSche, final IFiberActorHandler ah){
if (fibSche == null){
throw new NullPointerException();
}
final Actor actor = makeActor(fibSche);
ActorId aid = actor.getActorId();
new Fiber<Void>(fibSche) {
private static final long serialVersionUID = 2841359941298581576L;
@Override
protected Void run() throws SuspendExecution, InterruptedException {
Actor.runOnFiber(actor, ah);
return null;
}
}.start();
return aid;
}
示例6: reset
import co.paralleluniverse.fibers.Fiber; //导入依赖的package包/类
public void reset() {
int cnt = 0;
log("Waiting on [" + fibers.size() + "] Fibers....");
for(Fiber<?> f: fibers.values()) {
try {
while(f.getState()==Strand.State.RUNNING) {
SystemClock.sleep(100);
}
log("Testing State: %s : %s", f.getName(), f.getState());
cnt++;
} catch (Exception ex) {
ex.printStackTrace(System.err);
//throw new RuntimeException(ex);
}
}
fiberTask.set(0);
log("Reset Complete. Tasks:" + cnt);
invBuilder.build().send();
log("BulkRequest Dispatched");
}
示例7: testQuasar
import co.paralleluniverse.fibers.Fiber; //导入依赖的package包/类
@Test
public void testQuasar() throws ExecutionException, InterruptedException {
AsyncApi async = new AsyncApiImpl();
SyncApi sync = new SyncApiImpl(async);
new Fiber<Void>(() -> {
int a = new Random().nextInt(1000);
System.out.println("a is " + a);
int b = new Random().nextInt(1000);
System.out.println("b is " + b);
System.out.println(a + " + " + b + " = " + sync.sum(a, b));
}).start().join();
}
示例8: testDefaultSleep
import co.paralleluniverse.fibers.Fiber; //导入依赖的package包/类
@Test
public void testDefaultSleep() throws Exception {
final Script script = createScript("sleep(1000);", new HashMap<String, Object>());
final AtomicInteger counter = new AtomicInteger(0);
Fiber fiber = new Fiber(scheduler, new SuspendableRunnable() {
@Override
public void run() throws SuspendExecution, InterruptedException {
counter.incrementAndGet();
script.run();
counter.incrementAndGet();
}
});
fiber.start();
fiber.join();
Assert.assertEquals(counter.intValue(), 2);
}
示例9: testClosuresSleep
import co.paralleluniverse.fibers.Fiber; //导入依赖的package包/类
@Test
public void testClosuresSleep() throws Exception {
final SleepMethodSupport sleepMethodSupport = new SleepMethodSupport();
HashMap<String, Object> args = new HashMap<String, Object>() {
{
put("sleepMethodSupport", sleepMethodSupport);
put("_sleep", new MethodClosure(sleepMethodSupport, "_sleep"));
}
};
final Script script = createScript("sleep(1000);_sleep(1000);sleepMethodSupport._sleep(1000)", args);
final AtomicInteger counter = new AtomicInteger(0);
Fiber fiber = new Fiber(scheduler, new SuspendableRunnable() {
@Override
public void run() throws SuspendExecution, InterruptedException {
counter.incrementAndGet();
script.run();
counter.incrementAndGet();
}
});
fiber.start();
fiber.join();
Assert.assertEquals(counter.intValue(), 2);
}
示例10: testInFiber
import co.paralleluniverse.fibers.Fiber; //导入依赖的package包/类
private static void testInFiber() throws ExecutionException, InterruptedException {
final HystrixFiber hystrixFiber = new HystrixFiber();
// System.out.println(hystrixFiber.execute());
Fiber fiber = new Fiber<String>() {
@Override
@Suspendable
protected String run() throws SuspendExecution, InterruptedException {
System.out.println(Thread.currentThread().getName());
return hystrixFiber.execute();
}
}.start();
System.out.println(fiber.get());
}
示例11: execute
import co.paralleluniverse.fibers.Fiber; //导入依赖的package包/类
@Override
@Suspendable
public void execute(Runnable command) {
new Fiber<Void>() {
@Override
protected Void run() throws SuspendExecution, InterruptedException {
command.run();
return null;
}
}.start();
}
示例12: getFiber
import co.paralleluniverse.fibers.Fiber; //导入依赖的package包/类
private static Fiber getFiber(final long sleep, final String message) {
return new Fiber<String>() {
@Override
protected String run() throws SuspendExecution, InterruptedException {
Strand.sleep(sleep);
return message;
}
};
}
示例13: startFiber
import co.paralleluniverse.fibers.Fiber; //导入依赖的package包/类
private static void startFiber(FiberCompletionService completionService, String message, long sleep) {
new Fiber<Void>() {
@Override
protected Void run() throws SuspendExecution, InterruptedException {
completionService.submit(new FiberCallable(message, sleep));
return null;
}
}.start();
}
示例14: fiberSleep
import co.paralleluniverse.fibers.Fiber; //导入依赖的package包/类
@Suspendable
public static Long fiberSleep() {
try {
Fiber.sleep(1000);
return 1000l;
} catch (Exception e) {
throw new RuntimeException(e);
}
}
示例15: main
import co.paralleluniverse.fibers.Fiber; //导入依赖的package包/类
/**
* Simple syntax, but no performance gain
*/
public static void main(String[] args) throws Exception {
TaskRunner taskRunner = new TaskRunner(1000);
taskRunner.runTask(() -> new Fiber((SuspendableRunnable) () -> {
Big.task();
taskRunner.countDown();
}).start());
}