Last active
November 21, 2020 09:13
-
-
Save Restioson/fb5b92e16eaff3d9267024282cf1ed72 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
fun main(args: Array<String>) { | |
if (args[0] == "start") { | |
launch(CommonPool) { | |
println("Launching") | |
SerialisationHelper.test() | |
println("Finished") | |
} | |
} | |
else { | |
val coro = SerialisationHelper.deserialiseCoro() | |
coro.resume(Unit) | |
} | |
while (true) {} | |
} | |
object SerialisationHelper { | |
val kryo = Kryo() | |
init { | |
kryo.instantiatorStrategy = Kryo.DefaultInstantiatorStrategy(StdInstantiatorStrategy()) | |
} | |
suspend fun test(): Unit = suspendCoroutine { | |
println("Serialising") | |
testSerialise(it) | |
} | |
fun <T: Any> serialiseCoro(coro: Continuation<T>) { | |
val output = Output(FileOutputStream("coro.bin")) | |
kryo.writeClassAndObject(output, coro) | |
output.close() | |
} | |
fun deserialiseCoro(): Continuation<Any> { | |
println("Deserialising") | |
val fis = Input(FileInputStream("coro.bin")) | |
val coro = kryo.readClassAndObject(fis) as Continuation<Any> | |
fis.close() | |
coro::class.java.getDeclaredField("result").apply { | |
isAccessible = true | |
set(coro, COROUTINE_SUSPENDED) | |
} | |
return coro | |
} | |
fun <T: Any> testSerialise(coro: Continuation<T>) { | |
serialiseCoro(coro) | |
} | |
} |
This is a very old version of kotlin and likely no longer works anymore. For more up to date info I recommend checking out Kotlin/kotlinx.coroutines#76
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
What version of kotlin do you use?