本文整理汇总了Java中co.paralleluniverse.fibers.Suspendable类的典型用法代码示例。如果您正苦于以下问题:Java Suspendable类的具体用法?Java Suspendable怎么用?Java Suspendable使用的例子?那么, 这里精选的类代码示例或许可以为您提供帮助。
Suspendable类属于co.paralleluniverse.fibers包,在下文中一共展示了Suspendable类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: wait
import co.paralleluniverse.fibers.Suspendable; //导入依赖的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();
}
}
}
}
示例2: submit
import co.paralleluniverse.fibers.Suspendable; //导入依赖的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();
}
示例3: getPersonInCity
import co.paralleluniverse.fibers.Suspendable; //导入依赖的package包/类
@Suspendable
private static TypicalExamples.PersonInCity getPersonInCity(String id) {
if (!validateId(id)) {
throw new IllegalArgumentException("invalid id");
}
try {
InputStream personStream = client.execute(new HttpGet(url + "/person/" + id)).getEntity().getContent();
String personContent = new BufferedReader(new InputStreamReader(personStream)).readLine();
TypicalExamples.Person person = parsePerson(personContent);
InputStream addressStream = client.execute(new HttpGet(url + "/address/" + id)).getEntity().getContent();
String addressContent = new BufferedReader(new InputStreamReader(addressStream)).readLine();
TypicalExamples.Address address = parseAddress(addressContent);
return translate(person, address);
} catch (IOException e) {
throw new RuntimeException(e);
}
}
示例4: start
import co.paralleluniverse.fibers.Suspendable; //导入依赖的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);
}
示例5: testOn
import co.paralleluniverse.fibers.Suspendable; //导入依赖的package包/类
@Test
@Suspendable
public void testOn() throws Exception {
Channel<String> channel = Channels.newChannel(0);
r.on("image\\s+me\\s+ninjas", (c, request, o) -> channel.send("testOn"));
r.on("some other stuff", (con, request, c) -> fail("incorrect handler triggered"));
r.triggerHandlersForMessageText(sampleContext, "@Mars image me ninjas", new Response(Jid.of("[email protected]"), dummy));
String result;
int recv = 0;
while((result=channel.receive(defaultTimeout)) != null)
{
if (!result.equals("testOn")) {
fail("incorrect message received from event");
} else {
recv++;
}
}
assertTrue("correct message not received before timeout (or received more than once)", recv==1);
}
示例6: testWhenContains
import co.paralleluniverse.fibers.Suspendable; //导入依赖的package包/类
@Test
@Suspendable
public void testWhenContains() throws Exception {
Channel<String> channel = Channels.newChannel(0);
r.whenContains(".*turtles.*", (c, request, o) -> channel.send("done"));
r.on("some other stuff", (con, request, c) -> fail("incorrect handler triggered"));
r.triggerHandlersForMessageText(sampleContext, "the world loves some turtles now and again", new Response(Jid.of("[email protected]"), dummy));
String result;
int recv = 0;
while((result=channel.receive(defaultTimeout)) != null)
{
if (!result.equals("done")) {
fail("incorrect message received from event");
} else {
recv++;
}
}
assertTrue("correct message not received before timeout (or received more than once)", recv==1);
}
示例7: defineWord
import co.paralleluniverse.fibers.Suspendable; //导入依赖的package包/类
@Suspendable
public static void defineWord(String trigger, Response response) {
String word = readTrigger(trigger);
Optional<Definition> d;
try {
d = new DefinitionsRepo().getDefinition(word);
} catch (IOException e) {
LoggerFactory.getLogger(Define.class).error("couldn't get definitions from urban dictionary", e);
response.send("Sorry I can't help right now. Maybe my logs will be more helpful...");
return;
}
if (d.isPresent())
{
response.send(defineAsString(d.get()));
} else {
response.send("Sorry.. I don't think " + word + " is a real word.");
}
}
示例8: send
import co.paralleluniverse.fibers.Suspendable; //导入依赖的package包/类
@Suspendable
public <A, A1, A2, A3> void send(ActorId toAid, String type, A arg, A1 arg1, A2 arg2, A3 arg3){
if (arg == null){
throw new NullPointerException();
}
if (arg1 == null){
throw new NullPointerException();
}
if (arg2 == null){
throw new NullPointerException();
}
if (arg3 == null){
throw new NullPointerException();
}
Message msg = makeNewMessage();
msg.put(arg);
msg.put(arg1);
msg.put(arg2);
msg.put(arg3);
sendMessage(toAid, selfAid, type, msg);
}
示例9: testWhenContains
import co.paralleluniverse.fibers.Suspendable; //导入依赖的package包/类
@Test
@Suspendable
public void testWhenContains() throws Exception {
Channel<String> channel = Channels.newChannel(0);
r.whenContains(".*turtles.*", (request, o) -> channel.send("done"));
r.on("some other stuff", (request, c) -> fail("incorrect handler triggered"));
r.triggerHandlersForMessageText("the world loves some turtles now and again", new Response(Jid.valueOf("[email protected]"), dummy));
String result;
int recv = 0;
while((result=channel.receive(defaultTimeout)) != null)
{
if (!result.equals("done")) {
fail("incorrect message received from event");
} else {
recv++;
}
}
assertTrue("correct message not received before timeout (or received more than once)", recv==1);
}
示例10: get
import co.paralleluniverse.fibers.Suspendable; //导入依赖的package包/类
/**
* Returns the value to which the specified key is mapped, or {@code null}
* if this map contains no mapping for the key.
*
* <p>
* More formally, if this map contains a mapping from a key {@code k} to a
* value {@code v} such that {@code key.equals(k)}, then this method returns
* {@code v}; otherwise it returns {@code null}. (There can be at most one
* such mapping.)
*
* @throws NullPointerException
* if the specified key is null
*/
@SuppressWarnings("unchecked")
@Suspendable
public V get(Object key) {
Segment<K, V> s; // manually integrate access methods to reduce overhead
HashEntry<K, V>[] tab;
int h = hash(key);
long u = (((h >>> segmentShift) & segmentMask) << SSHIFT) + SBASE;
if ((s = (Segment<K, V>) UNSAFE.getObjectVolatile(segments, u)) != null
&& (tab = s.table) != null) {
for (HashEntry<K, V> e = (HashEntry<K, V>) UNSAFE
.getObjectVolatile(tab,
((long) (((tab.length - 1) & h)) << TSHIFT) + TBASE); e != null; e = e.next) {
K k;
if ((k = e.key) == key || (e.hash == h && key.equals(k)))
return e.value;
}
}
return null;
}
示例11: spawn
import co.paralleluniverse.fibers.Suspendable; //导入依赖的package包/类
/**
* 产生一个线程actor,并且使用指定的Handler来放入指定的执行器执行
* @param executor 执行器
* @param ah 可运行单元
* @return 新Actor的Id
*/
@Suspendable
public ActorId spawn(Executor executor, final IThreadActorHandler ah){
if (executor == null){
throw new NullPointerException();
}
final Actor actor = makeActor(executor);
ActorId aid = actor.getActorId();
executor.execute(new Runnable() {
@Override
public void run() {
Actor.runOnThread(actor, ah);
}
});
return aid;
}
示例12: removeRemote
import co.paralleluniverse.fibers.Suspendable; //导入依赖的package包/类
@Suspendable
public boolean removeRemote(long axid){
NetSession netSession = removeNetSession(axid);
if (netSession != null){
ActorId handleAid = netSession.getHandleAid();
netSession.close();
if (!ActorId.equals(handleAid, lisnAid)){
selfAx.send(handleAid, "STOP");
try {
selfAx.recvExit(handleAid);
} catch (InterruptedException e) {
}
}
return true;
}
return false;
}
示例13: send
import co.paralleluniverse.fibers.Suspendable; //导入依赖的package包/类
@Suspendable
public <A, A1, A2> void send(ActorId fromAid, ActorId toAid, String type, A arg, A1 arg1, A2 arg2){
if (arg == null){
throw new NullPointerException();
}
if (arg1 == null){
throw new NullPointerException();
}
if (arg2 == null){
throw new NullPointerException();
}
Message msg = Message.make();
msg.put(arg);
msg.put(arg1);
msg.put(arg2);
msg.setSender(fromAid);
msg.setType(type);
if (!Actor.sendMessage(this, toAid, msg)){
msg.release();
}
}
示例14: testInFiber
import co.paralleluniverse.fibers.Suspendable; //导入依赖的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());
}
示例15: execute
import co.paralleluniverse.fibers.Suspendable; //导入依赖的package包/类
@Override
@Suspendable
public void execute(Runnable command) {
new Fiber<Void>() {
@Override
protected Void run() throws SuspendExecution, InterruptedException {
command.run();
return null;
}
}.start();
}