Last active
August 29, 2015 14:12
-
-
Save karupanerura/b0d524aae2d7fce4a432 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class AsyncExample { | |
public void run () { | |
Client client = new Client(); | |
Listener listener = new Listener () { | |
@Override | |
public void onSuccess (Result result) { | |
...; | |
} | |
}; | |
client.request("SELECT * FROM foo WHERE id = 1", listener); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class Client { | |
... | |
public void request (String query, Listener listener) { | |
queue.add(query, listener); // ここにつっこむとWorker Threadがよしなに処理してlistener.onSuccess()を呼び出してくれる的なやつ | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public abstract class Listener { | |
public abstract void onSuccess (Result result); | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import java.util.concurrent.Future; | |
public class ListenerFuture extends Listener implements Future<Result> { | |
private Result result; | |
public Result get() { | |
...; // 実行が終わるまで待つ処理 | |
return result; | |
} | |
... | |
@Override | |
public void onSuccess (Result result) { | |
this.result = result; | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class SyncExample { | |
public void run () { | |
Client client = new Client(); | |
ListenerFuture listener = new ListenerFuture(); | |
client.request("SELECT * FROM foo WHERE id = 1", listener); | |
Result result = listener.get(); | |
...; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment