わんくまでは珍しいJavaを中心とした日記です
前回のエントリのコードは不完全だったのとイテレータ限定だったので、キチンとしたContinuationのクラスを書いてみました。それをラップするイテレータも作りました。
http://herringroe.googlecode.com/svn/trunk/herringroe/src/sandbox/main/java/jp/herringroe/commons/util/ContinuationIterator.java
http://herringroe.googlecode.com/svn/trunk/herringroe/src/sandbox/main/java/jp/herringroe/commons/util/continuation/Continuation.java
http://herringroe.googlecode.com/svn/trunk/herringroe/src/sandbox/main/java/jp/herringroe/commons/util/continuation/Coroutine.java
http://herringroe.googlecode.com/svn/trunk/herringroe/src/sandbox/main/java/jp/herringroe/commons/util/continuation/CoroutineException.java
http://herringroe.googlecode.com/svn/trunk/herringroe/src/sandbox/main/java/jp/herringroe/commons/util/continuation/Result.java
http://herringroe.googlecode.com/svn/trunk/herringroe/src/sandbox/main/java/jp/herringroe/commons/util/continuation/Yieldable.java
処理の書き方が下手なのか、そもそもそんなものなのか分かりませんが、かなり重いです。チューニングの余地はあるかも。
継続処理を行うには、呼び出し元スレッドだけではだめで、コルーチン側にもスレッドが必要になります。呼び出し元スレッドによるコルーチン側の呼び出しと、コルーチン側のスレッドでのyield呼び出しで、スレッドを交互に待機させます。単純にロックオブジェクトのモニタ取得、解放、待ちを交互に行うだけだと、タイミング次第では直ぐにデッドロックしてしまう為、2重に同期化してから解放、待ちを繰り返すようにしています。多分その辺が重いのかも。
重いのでクリティカルでループ回数の多い処理は向きませんが、タイミングを制御することなくプッシュ型で結果を配信できるので、ストリーム風の処理に向いていると思います。
投稿日時 : 2007年9月21日 3:22
Powered by: Copyright © かつのり