Project Loomの(プロトタイプ的な)ソースコードが公開されました。chiroitoさんがさっそく試されています。
記事はLinuxですが、私もmacOSで動作させることができました。
Project Loomって何?という方はこちらをご覧ください。
ビルド
chiroitoさんの記事にあるものと同じです。
$ hg clone http://hg.openjdk.java.net/loom/loom $ cd loom $ hg update -r fibers $ sh configure $ make images ... Finished building target 'images' in configuration 'macosx-x86_64-normal-server-release'
Fiberの実行
chiroitoさんがサンプルソースコードも作成してくださっています。
public class FiberSandbox { public static void main(String... args){ Fiber f = Fiber.execute(()->{ System.out.println("Hello Fiber !"); }); f.await(); } }
https://github.com/chiroito/sandbox/blob/master/sandbox-loom/src/main/java/FiberSandbox.java
build/macosx-x86_64-normal-server-release/images/jdk
がJDKの場所となります。
$ build/macosx-x86_64-normal-server-release/images/jdk/bin/javac FiberSandbox.java $ build/macosx-x86_64-normal-server-release/images/jdk/bin/java FiberSandbox Hello Fiber !
Fiberクラス
src/java.base/share/classes/java/lang/Fiber.java
です。
/** * A lightweight strand. A Fiber is a <i>user mode</i> strand, it is always * scheduled by the Java virtual machine rather than the operating system. */ public final class Fiber extends Strand { // *snip* }
「軽量なstrand。Fiberはユーザモードのstrandであり、つねにJava仮想マシンによってスケジュールされる。OSによってではない。」と書かれています。
いくつかあったLoomの設計方針から、現時点ではFiberとThreadの共通親クラスとしてStrandを作る方針を選択したようです。
src/java.base/share/classes/java/lang/Strand.java
です。
/** * A sequence of computer instructions executed sequentially, either a {@code * Thread} or a light weight {@code Fiber}. * * @apiNote This is a class rather than an interface for now. The constructor is * not public (or protected) so that it can't be extended outside of the java.lang * package. */ public class Strand { // *snip* }
「スレッドか軽量なFiberのどちらかでシーケンシャルに実行されるコンピュータ命令のシーケンス。今のところインタフェースではなくクラスである。コンストラクタはpublicではない(protectedでもない)。java.langパッケージ外では継承できないようにするためだ。」
src/java.base/share/classes/java/lang/Thread.java
です。
public class Thread extends Strand implements Runnable { // *snip* }
Strandを継承するようになっています。