Skip to content
Permalink
Browse files

Thread and builder API

  • Loading branch information
AlanBateman committed Nov 12, 2019
1 parent 0fd9645 commit 3c0de18772f05201d1979ec71c06935dff3809c1
@@ -55,7 +55,8 @@
import static java.util.concurrent.TimeUnit.NANOSECONDS;

/**
* Lightweight thread implementation.
* A thread that is scheduled by the Java virtual machine rather than the operating
* system.
*/

class Fiber extends Thread {
@@ -110,11 +111,9 @@
* @param characteristics characteristics
* @param task the task to execute
*/
@SuppressWarnings("unchecked")
Fiber(Executor scheduler, String name, int characteristics, Runnable task) {
super(name, characteristics);

Objects.requireNonNull(scheduler);
Objects.requireNonNull(task);

Runnable target = () -> {
@@ -130,7 +129,7 @@
}
};

this.scheduler = scheduler;
this.scheduler = (scheduler != null) ? scheduler : DEFAULT_SCHEDULER;
this.cont = new Continuation(FIBER_SCOPE, target) {
@Override
protected void onPinned(Continuation.Pinned reason) {
@@ -146,10 +145,6 @@ protected void onPinned(Continuation.Pinned reason) {
this.runContinuation = this::runContinuation;
}

Fiber(String name, int characteristics, Runnable task) {
this(DEFAULT_SCHEDULER, name, characteristics, task);
}

/**
* Schedules this {@code Fiber} to execute.
*
@@ -632,7 +627,7 @@ void sleepNanos(long nanos) throws InterruptedException {

@Override
public String toString() {
StringBuilder sb = new StringBuilder("LightweightThread[");
StringBuilder sb = new StringBuilder("VirtualThread[");
String name = getName();
if (name.length() > 0) {
sb.append(name);

Large diffs are not rendered by default.

@@ -271,7 +271,7 @@ public void remove() {
ThreadLocalMap getMap(Thread t) {
ThreadLocalMap map = t.threadLocals;
if (map == ThreadLocalMap.NOT_SUPPORTED) {
throw new UnsupportedOperationException();
throw new UnsupportedOperationException("Thread locals not supported");
}
return map;
}
@@ -65,9 +65,15 @@
};

public static void test1() throws Exception {
Thread.newLightWeightThread(0, PRODUCER).start();
Thread.newLightWeightThread(0, CONSUMER).start();
Thread.newLightWeightThread(0, CONSUMER).start();
Thread producer = Thread.newThread(Thread.LIGHTWEIGHT, PRODUCER);
Thread conusmer1 = Thread.newThread(Thread.LIGHTWEIGHT, CONSUMER);
Thread consumer2 = Thread.newThread(Thread.LIGHTWEIGHT, CONSUMER);
producer.start();
consumer1.start();
consumer2.start();
producer.join();
consumer1.join();
consumer2.join();
}

void runTest() throws Exception {
@@ -65,8 +65,12 @@ static void producer(String msg) throws InterruptedException {
};

public static void test1() throws Exception {
Thread.newLightWeightThread(0, PRODUCER).start();
Thread.newLightWeightThread(0, CONSUMER).start();
Thread producer = Thread.newThread(Thread.LIGHTWEIGHT, PRODUCER);
Thread consumer = Thread.newThread(Thread.LIGHTWEIGHT, CONSUMER);
producer.start();
consumer.start();
producer.join();
consumer.join();
}

void runTest() throws Exception {

0 comments on commit 3c0de18

Please sign in to comment.
You can’t perform that action at this time.