/** * Returns a strand representing the given thread. */ public static Strand of(Thread thread) { return Strand.of(thread); }
public static Strand of(Object owner) { if (owner instanceof Strand) return (Strand) owner; // if (owner instanceof Fiber) // return (Fiber) owner; else return of((Thread) owner); }
/** * Returns a strand representing the given fiber. * The current implementation simply returns the fiber itself as {@code Fiber} extends {@code Fiber}. */ public static Strand of(Fiber fiber) { return Strand.of(fiber); }
public static Strand of(Object owner) { return Strand.of(owner); }
/** * Tests whether two strands represent the same fiber or thread. * * @param strand1 May be an object of type {@code Strand}, {@code Fiber} or {@code Thread}. * @param strand2 May be an object of type {@code Strand}, {@code Fiber} or {@code Thread}. * @return {@code true} if the two strands represent the same fiber or the same thread; {@code false} otherwise. */ public static boolean equals(Object strand1, Object strand2) { if (strand1 == strand2) return true; if (strand1 == null | strand2 == null) return false; return of(strand1).equals(of(strand2)); }
@Override public void uncaughtException(Thread t, Throwable e) { uncaughtExceptionHandler.uncaughtException(Strand.of(t), e); } });
@Override public Strand newStrand(SuspendableCallable<?> target) { return Strand.of(tf.newThread(Strand.toRunnable(target))); } };
/** * Awaits the termination of a given strand, at most for the timeout duration specified. * This method blocks until this strand terminates or the timeout elapses. * * @param strand the strand to join. May be an object of type {@code Strand}, {@code Fiber} or {@code Thread}. * @param timeout the maximum duration to wait for the strand to terminate in the time unit specified by {@code unit}. * @param unit the time unit of {@code timeout}. * * @throws TimeoutException if this strand did not terminate by the time the timeout has elapsed. * @throws ExecutionException if this strand has terminated as a result of an uncaught exception * (which will be the {@link Throwable#getCause() cause} of the thrown {@code ExecutionException}. * @throws InterruptedException */ public static void join(Object strand, long timeout, TimeUnit unit) throws ExecutionException, InterruptedException, TimeoutException { if (strand instanceof Strand) ((Strand) strand).join(timeout, unit); else if (strand instanceof Thread) join(Strand.of(strand), timeout, unit); else throw new IllegalArgumentException("Can't join an object of type " + strand.getClass()); }
@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; } };
/** * Starts a new thread and runs the actor in it. * The fiber's name will be set to this actor's name. * * @return This actors' ActorRef */ public ActorRef<Message> spawnThread() { checkReplacement(); Runnable runnable = Strand.toRunnable(runner); Thread t = (getName() != null ? new Thread(runnable, getName()) : new Thread(runnable)); setStrand(Strand.of(t)); t.start(); return ref(); }