本文整理汇总了Java中co.paralleluniverse.fibers.SuspendExecution类的典型用法代码示例。如果您正苦于以下问题:Java SuspendExecution类的具体用法?Java SuspendExecution怎么用?Java SuspendExecution使用的例子?那么, 这里精选的类代码示例或许可以为您提供帮助。
SuspendExecution类属于co.paralleluniverse.fibers包,在下文中一共展示了SuspendExecution类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: resumeBuilding
import co.paralleluniverse.fibers.SuspendExecution; //导入依赖的package包/类
protected void resumeBuilding(Building construction) throws InterruptedException, SuspendExecution {
this.available = false;
boolean success = execute(new ResumeBuildingCommand(this.scv, construction));
while(construction.exists() && !construction.isCompleted() && this.alive) {
Message message = receive();
if (message instanceof FrameUpdate) {
update((FrameUpdate)message);
if (!success) {
success = execute(new ResumeBuildingCommand(this.scv, construction));
}
}
this.alive &= this.scv.exists();
}
this.available = true;
}
示例2: main
import co.paralleluniverse.fibers.SuspendExecution; //导入依赖的package包/类
public static void main(String[] args) throws ExecutionException, InterruptedException, SuspendExecution {
int FiberNumber = 1_00_000;
CountDownLatch latch = new CountDownLatch(1);
AtomicInteger counter = new AtomicInteger(0);
for (int i = 0; i < FiberNumber; i++) {
new Fiber(() -> {
counter.incrementAndGet();
if (counter.get() == FiberNumber) {
System.out.println("done");
}
Strand.sleep(Integer.MAX_VALUE);
}).start();
}
latch.await();
}
示例3: wait
import co.paralleluniverse.fibers.SuspendExecution; //导入依赖的package包/类
@Suspendable
public void wait(int totalCount, long timeout) throws TimeoutException, InterruptedException, ExecutionException {
long startTime = System.currentTimeMillis();
for (int i = 0; i < totalCount; i++) {
long currentTime = System.currentTimeMillis();
long elapsedTime = currentTime - startTime;
long remainingTime = timeout - elapsedTime;
if (remainingTime > 0) {
try {
Future<T> future = channel.receive(remainingTime, TimeUnit.MILLISECONDS);
if (future == null) {
throw new TimeoutException();
}
future.get();
} catch (SuspendExecution suspendExecution) {
suspendExecution.printStackTrace();
}
}
}
}
示例4: submit
import co.paralleluniverse.fibers.SuspendExecution; //导入依赖的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();
}
示例5: start
import co.paralleluniverse.fibers.SuspendExecution; //导入依赖的package包/类
@Suspendable
@Override
public void start() throws Exception {
httpServer = vertx.createHttpServer();
httpServer.requestHandler(
Sync.fiberHandler(req -> {
try {
Strand.sleep(1, TimeUnit.SECONDS); // 1
} catch (SuspendExecution | InterruptedException e) { // 1
e.printStackTrace(); // 1
} // 1
//sleep(); // 2
final String body = FiberHttpServer.class.getName();
req.response()
.putHeader("Content-Length", String.valueOf(body.length()))
.end(body);
})).listen(port);
}
示例6: main
import co.paralleluniverse.fibers.SuspendExecution; //导入依赖的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);
}
示例7: spawn
import co.paralleluniverse.fibers.SuspendExecution; //导入依赖的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;
}
示例8: handlePush
import co.paralleluniverse.fibers.SuspendExecution; //导入依赖的package包/类
private void handlePush(MinaPushMessage push) throws SuspendExecution {
try{
ActorId toAid = push.getToAid();
Message rmsg = push.getMessage();
MinaMsgCode msgCode = push.getMsgCode();
switch (msgCode){
case EXIT_PUSH:{
actorExit(rmsg.getSender(), toAid);
axSys.send(toAid, rmsg);
}break;
case SEND_PUSH:{
axSys.send(toAid, rmsg);
}break;
default:
throw new AssertionError(msgCode);
}
}finally{
push.release();
}
}
示例9: handlePush
import co.paralleluniverse.fibers.SuspendExecution; //导入依赖的package包/类
private void handlePush(IoSession session, MinaPushMessage push) throws SuspendExecution {
try{
ActorId toAid = push.getToAid();
Message rmsg = push.getMessage();
MinaMsgCode msgCode = push.getMsgCode();
switch (msgCode){
case EXIT_PUSH:{
actorExit(rmsg.getSender(), toAid);
axSys.send(toAid, rmsg);
}break;
case SEND_PUSH:{
axSys.send(toAid, rmsg);
}break;
default:
throw new AssertionError(msgCode);
}
}finally{
push.release();
}
}
示例10: awaitNanos
import co.paralleluniverse.fibers.SuspendExecution; //导入依赖的package包/类
@Override
@Suspendable
public long awaitNanos(long nanos) throws InterruptedException {
long left = nanos;
long deadline = System.nanoTime() + left;
try {
Strand.parkNanos(this, left);
} catch (SuspendExecution e) {
throw new AssertionError();
}
if (Strand.interrupted()){
throw new InterruptedException();
}
left = deadline - System.nanoTime();
return left;
}
示例11: get
import co.paralleluniverse.fibers.SuspendExecution; //导入依赖的package包/类
@SuppressWarnings("unchecked")
@Suspendable
public <T> T get(final Class<?> type) {
try {
return (T)run(); //(T) fiberPool.newFiber(new AsyncDelegator(this, type)).start().get();
} catch (Throwable e) {
System.out.println("Async Ex:" + e);
e.printStackTrace();
if(e.getClass()!=SuspendExecution.class) {
// UnsafeAdapter.throwException(e);
throw new RuntimeException(); // won't get called
} else {
return null; // won't get called
}
}
}
示例12: publish
import co.paralleluniverse.fibers.SuspendExecution; //导入依赖的package包/类
@Override
@Suspendable
@SuppressWarnings("all")
public <E extends Event> void publish(E event) {
if (event != null) {
EventSource<Event> eventSource = (EventSource<Event>) eventSourceMap.get(event.getClass());
if (eventSource != null) {
try {
eventSource.notify(event);
} catch (SuspendExecution e) {
throw RuntimeSuspendExecution.of(e);
}
}
}
}
示例13: sleepImpl
import co.paralleluniverse.fibers.SuspendExecution; //导入依赖的package包/类
/**
* This method is used by both sleep() methods to implement sleeping
* for the given time even if interrupted
*
* @param millis the number of milliseconds to sleep
* @param closure optional closure called when interrupted
* as long as the closure returns false the sleep continues
*/
private static void sleepImpl(long millis, Closure closure) throws SuspendExecution{
long start = System.currentTimeMillis();
long rest = millis;
long current;
while (rest > 0) {
try {
Strand.sleep(rest);
rest = 0;
} catch (InterruptedException e) {
if (closure != null) {
if (DefaultTypeTransformation.castToBoolean(closure.call(e))) {
return;
}
}
current = System.currentTimeMillis(); // compensate for closure's time
rest = millis + start - current;
}
}
}
示例14: testDefaultSleep
import co.paralleluniverse.fibers.SuspendExecution; //导入依赖的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);
}
示例15: testClosuresSleep
import co.paralleluniverse.fibers.SuspendExecution; //导入依赖的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);
}