diff --git a/build.gradle.kts b/build.gradle.kts new file mode 100644 index 0000000..8406961 --- /dev/null +++ b/build.gradle.kts @@ -0,0 +1,20 @@ +plugins{ + kotlin("jvm") version "2.0.20" +} + +repositories{ + mavenCentral() +} + +val ktorVersion = "3.0.1" + +dependencies { + implementation("org.jetbrains.kotlinx:kotlinx-coroutines-core:1.9.0") + + implementation("io.ktor:ktor-client-core:$ktorVersion") + implementation("io.ktor:ktor-client-cio:$ktorVersion") + implementation("io.ktor:ktor-client-core-jvm:3.0.1") + implementation("io.ktor:ktor-client-apache:3.0.1") + + implementation("ch.qos.logback:logback-classic:1.5.12") +} \ No newline at end of file diff --git a/notebooks/Multitasking.ipynb b/notebooks/Multitasking.ipynb index 5c85e84..3278a8e 100644 --- a/notebooks/Multitasking.ipynb +++ b/notebooks/Multitasking.ipynb @@ -2,201 +2,267 @@ "cells": [ { "cell_type": "code", - "source": [ - "%use coroutines" - ], + "execution_count": null, "metadata": { "datalore": { - "node_id": "Al3t64xqN0DN3JmLKHOVn2", - "type": "CODE", "hide_input_from_viewers": true, - "hide_output_from_viewers": true + "hide_output_from_viewers": true, + "node_id": "Al3t64xqN0DN3JmLKHOVn2", + "type": "CODE" } }, "outputs": [], - "execution_count": null + "source": [ + "%use coroutines" + ] }, { + "cell_type": "markdown", "metadata": {}, - "cell_type": "markdown", - "source": "# Три оси многозадачности" + "source": [ + "# Три оси многозадачности" + ] }, { - "cell_type": "markdown", - "source": "### Синхронный х однопоточный х неконкурентный ", "attachments": {}, + "cell_type": "markdown", "metadata": { "datalore": { - "node_id": "TawmM9V8by8RGHfhNv7D3m", - "type": "MD", "hide_input_from_viewers": true, - "hide_output_from_viewers": true + "hide_output_from_viewers": true, + "node_id": "TawmM9V8by8RGHfhNv7D3m", + "type": "MD" } - } + }, + "source": [ + "### Синхронный х однопоточный х неконкурентный " + ] }, { "cell_type": "code", + "execution_count": null, + "metadata": { + "datalore": { + "hide_input_from_viewers": true, + "hide_output_from_viewers": true, + "node_id": "XzjwoelmODMg8pmaJRe1KK", + "type": "CODE" + } + }, + "outputs": [], "source": [ "repeat(10){\n", " println(\"Line number $it\")\n", "}" - ], - "metadata": { - "datalore": { - "node_id": "XzjwoelmODMg8pmaJRe1KK", - "type": "CODE", - "hide_input_from_viewers": true, - "hide_output_from_viewers": true - } - }, - "outputs": [], - "execution_count": null + ] }, { "cell_type": "code", + "execution_count": null, + "metadata": { + "datalore": { + "hide_input_from_viewers": true, + "hide_output_from_viewers": true, + "node_id": "UuTmCNAMCpUAHaAwWVYJPy", + "type": "CODE" + } + }, + "outputs": [], "source": [ "import java.util.stream.*\n", "\n", "IntStream.range(0, 100).sum()" - ], - "metadata": { - "datalore": { - "node_id": "UuTmCNAMCpUAHaAwWVYJPy", - "type": "CODE", - "hide_input_from_viewers": true, - "hide_output_from_viewers": true - } - }, - "outputs": [], - "execution_count": null + ] }, { - "cell_type": "markdown", - "source": "### Синхронный х однопоточный х конкурентный", "attachments": {}, + "cell_type": "markdown", "metadata": { "datalore": { - "node_id": "f6DGHwBOIRvKuYjtxVRn0W", - "type": "MD", "hide_input_from_viewers": true, - "hide_output_from_viewers": true + "hide_output_from_viewers": true, + "node_id": "f6DGHwBOIRvKuYjtxVRn0W", + "type": "MD" } - } + }, + "source": [ + "### Синхронный х однопоточный х конкурентный" + ] }, { "cell_type": "code", + "execution_count": 1, + "metadata": { + "datalore": { + "hide_input_from_viewers": true, + "hide_output_from_viewers": true, + "node_id": "O1DPfMSAedOChaqU5JWXrU", + "type": "CODE" + } + }, + "outputs": [ + { + "name": "stderr", + "output_type": "stream", + "text": [ + "null\n", + "java.util.ConcurrentModificationException\n", + "\tat java.base/java.util.ArrayList$Itr.checkForComodification(ArrayList.java:1095)\n", + "\tat java.base/java.util.ArrayList$Itr.next(ArrayList.java:1049)\n", + "\tat Line_0_jupyter.(Line_0.jupyter.kts:6)\n", + "\tat java.base/jdk.internal.reflect.DirectConstructorHandleAccessor.newInstance(DirectConstructorHandleAccessor.java:62)\n", + "\tat java.base/java.lang.reflect.Constructor.newInstanceWithCaller(Constructor.java:502)\n", + "\tat java.base/java.lang.reflect.Constructor.newInstance(Constructor.java:486)\n", + "\tat kotlin.script.experimental.jvm.BasicJvmScriptEvaluator.evalWithConfigAndOtherScriptsResults(BasicJvmScriptEvaluator.kt:122)\n", + "\tat kotlin.script.experimental.jvm.BasicJvmScriptEvaluator.invoke$suspendImpl(BasicJvmScriptEvaluator.kt:48)\n", + "\tat kotlin.script.experimental.jvm.BasicJvmScriptEvaluator.invoke(BasicJvmScriptEvaluator.kt)\n", + "\tat kotlin.script.experimental.jvm.BasicJvmReplEvaluator.eval(BasicJvmReplEvaluator.kt:49)\n", + "\tat org.jetbrains.kotlinx.jupyter.repl.impl.InternalEvaluatorImpl$eval$resultWithDiagnostics$1.invokeSuspend(InternalEvaluatorImpl.kt:127)\n", + "\tat kotlin.coroutines.jvm.internal.BaseContinuationImpl.resumeWith(ContinuationImpl.kt:33)\n", + "\tat kotlinx.coroutines.DispatchedTask.run(DispatchedTask.kt:104)\n", + "\tat kotlinx.coroutines.EventLoopImplBase.processNextEvent(EventLoop.common.kt:277)\n", + "\tat kotlinx.coroutines.BlockingCoroutine.joinBlocking(Builders.kt:95)\n", + "\tat kotlinx.coroutines.BuildersKt__BuildersKt.runBlocking(Builders.kt:69)\n", + "\tat kotlinx.coroutines.BuildersKt.runBlocking(Unknown Source)\n", + "\tat kotlinx.coroutines.BuildersKt__BuildersKt.runBlocking$default(Builders.kt:48)\n", + "\tat kotlinx.coroutines.BuildersKt.runBlocking$default(Unknown Source)\n", + "\tat org.jetbrains.kotlinx.jupyter.repl.impl.InternalEvaluatorImpl.eval(InternalEvaluatorImpl.kt:127)\n", + "\tat org.jetbrains.kotlinx.jupyter.repl.impl.CellExecutorImpl$execute$1$result$1.invoke(CellExecutorImpl.kt:79)\n", + "\tat org.jetbrains.kotlinx.jupyter.repl.impl.CellExecutorImpl$execute$1$result$1.invoke(CellExecutorImpl.kt:77)\n", + "\tat org.jetbrains.kotlinx.jupyter.repl.impl.ReplForJupyterImpl.withHost(ReplForJupyterImpl.kt:758)\n", + "\tat org.jetbrains.kotlinx.jupyter.repl.impl.CellExecutorImpl.execute(CellExecutorImpl.kt:77)\n", + "\tat org.jetbrains.kotlinx.jupyter.repl.execution.CellExecutor$DefaultImpls.execute$default(CellExecutor.kt:12)\n", + "\tat org.jetbrains.kotlinx.jupyter.repl.impl.ReplForJupyterImpl.evaluateUserCode(ReplForJupyterImpl.kt:581)\n", + "\tat org.jetbrains.kotlinx.jupyter.repl.impl.ReplForJupyterImpl.access$evaluateUserCode(ReplForJupyterImpl.kt:136)\n", + "\tat org.jetbrains.kotlinx.jupyter.repl.impl.ReplForJupyterImpl$evalEx$1.invoke(ReplForJupyterImpl.kt:439)\n", + "\tat org.jetbrains.kotlinx.jupyter.repl.impl.ReplForJupyterImpl$evalEx$1.invoke(ReplForJupyterImpl.kt:436)\n", + "\tat org.jetbrains.kotlinx.jupyter.repl.impl.ReplForJupyterImpl.withEvalContext(ReplForJupyterImpl.kt:417)\n", + "\tat org.jetbrains.kotlinx.jupyter.repl.impl.ReplForJupyterImpl.evalEx(ReplForJupyterImpl.kt:436)\n", + "\tat org.jetbrains.kotlinx.jupyter.messaging.IdeCompatibleMessageRequestProcessor$processExecuteRequest$1$response$1$1.invoke(IdeCompatibleMessageRequestProcessor.kt:140)\n", + "\tat org.jetbrains.kotlinx.jupyter.messaging.IdeCompatibleMessageRequestProcessor$processExecuteRequest$1$response$1$1.invoke(IdeCompatibleMessageRequestProcessor.kt:139)\n", + "\tat org.jetbrains.kotlinx.jupyter.execution.JupyterExecutorImpl$Task.execute(JupyterExecutorImpl.kt:42)\n", + "\tat org.jetbrains.kotlinx.jupyter.execution.JupyterExecutorImpl$executorThread$1.invoke(JupyterExecutorImpl.kt:82)\n", + "\tat org.jetbrains.kotlinx.jupyter.execution.JupyterExecutorImpl$executorThread$1.invoke(JupyterExecutorImpl.kt:80)\n", + "\tat kotlin.concurrent.ThreadsKt$thread$thread$1.run(Thread.kt:30)\n" + ] + } + ], "source": [ "val list = (0..100).toMutableList()\n", "\n", - "list.forEach{\n", + "list.forEach {\n", " if(it % 2 == 0) list.remove(it)\n", "}" - ], - "metadata": { - "datalore": { - "node_id": "O1DPfMSAedOChaqU5JWXrU", - "type": "CODE", - "hide_input_from_viewers": true, - "hide_output_from_viewers": true - } - }, - "outputs": [], - "execution_count": null + ] }, { "cell_type": "code", - "source": [], + "execution_count": null, "metadata": { "datalore": { + "hide_input_from_viewers": true, + "hide_output_from_viewers": true, "node_id": "N3QIwvANEngnvMgsCVJatN", - "type": "CODE", - "hide_input_from_viewers": true, - "hide_output_from_viewers": true + "type": "CODE" } }, "outputs": [], - "execution_count": null + "source": [] }, { - "cell_type": "markdown", - "source": "### Синхронный х многопоточный х неконкурентный", "attachments": {}, + "cell_type": "markdown", "metadata": { "datalore": { - "node_id": "x9nGAXOgOlWxd4cu6Smio8", - "type": "MD", "hide_input_from_viewers": true, - "hide_output_from_viewers": true + "hide_output_from_viewers": true, + "node_id": "x9nGAXOgOlWxd4cu6Smio8", + "type": "MD" } - } + }, + "source": [ + "### Синхронный х многопоточный х неконкурентный" + ] }, { "cell_type": "code", + "execution_count": null, + "metadata": { + "datalore": { + "hide_input_from_viewers": true, + "hide_output_from_viewers": true, + "node_id": "lRJGB0cHhpjQTdRdGynG0G", + "type": "CODE" + } + }, + "outputs": [], "source": [ "val sum = IntStream.range(0, 100).parallel().sum()\n", "\n", "println(sum)" - ], - "metadata": { - "datalore": { - "node_id": "lRJGB0cHhpjQTdRdGynG0G", - "type": "CODE", - "hide_input_from_viewers": true, - "hide_output_from_viewers": true - } - }, - "outputs": [], - "execution_count": null + ] }, { "cell_type": "code", - "source": [], + "execution_count": null, "metadata": { "datalore": { + "hide_input_from_viewers": true, + "hide_output_from_viewers": true, "node_id": "1lLEGWWbB6qnFr1qjfSGpo", - "type": "CODE", - "hide_input_from_viewers": true, - "hide_output_from_viewers": true + "type": "CODE" } }, "outputs": [], - "execution_count": null + "source": [] }, { - "cell_type": "markdown", - "source": "### Асинхронный х однопоточный х неконкурентный", "attachments": {}, + "cell_type": "markdown", "metadata": { "datalore": { - "node_id": "OTRLxfWLADkMoc9ViJfeEq", - "type": "MD", "hide_input_from_viewers": true, - "hide_output_from_viewers": true + "hide_output_from_viewers": true, + "node_id": "OTRLxfWLADkMoc9ViJfeEq", + "type": "MD" } - } + }, + "source": [ + "### Асинхронный х однопоточный х неконкурентный" + ] }, { "cell_type": "code", + "execution_count": null, + "metadata": { + "datalore": { + "hide_input_from_viewers": true, + "hide_output_from_viewers": true, + "node_id": "r1yVVtgMhexxzx7gGVmeIg", + "type": "CODE" + } + }, + "outputs": [], "source": [ "fun runLongTask(result: Int = 8): Int {\n", " Thread.sleep(100)\n", " println(\"Task complete: $result\")\n", " return result\n", "}" - ], - "metadata": { - "datalore": { - "node_id": "r1yVVtgMhexxzx7gGVmeIg", - "type": "CODE", - "hide_input_from_viewers": true, - "hide_output_from_viewers": true - } - }, - "outputs": [], - "execution_count": null + ] }, { "cell_type": "code", + "execution_count": null, + "metadata": { + "datalore": { + "hide_input_from_viewers": true, + "hide_output_from_viewers": true, + "node_id": "8YvIKfD1I7cfbiPRMpk0DC", + "type": "CODE" + } + }, + "outputs": [], "source": [ "import java.util.concurrent.*\n", "\n", @@ -209,100 +275,104 @@ "}\n", "\n", "future.get()" - ], - "metadata": { - "datalore": { - "node_id": "8YvIKfD1I7cfbiPRMpk0DC", - "type": "CODE", - "hide_input_from_viewers": true, - "hide_output_from_viewers": true - } - }, - "outputs": [], - "execution_count": null + ] }, { - "cell_type": "markdown", - "source": "### Асинхронный х однопоточный х конкурентный ", "attachments": {}, + "cell_type": "markdown", "metadata": { "datalore": { - "node_id": "nyjKPayaltq8gBzq2bIP1c", - "type": "MD", "hide_input_from_viewers": true, - "hide_output_from_viewers": true + "hide_output_from_viewers": true, + "node_id": "nyjKPayaltq8gBzq2bIP1c", + "type": "MD" } - } + }, + "source": [ + "### Асинхронный х однопоточный х конкурентный " + ] }, { "cell_type": "code", + "execution_count": null, + "metadata": { + "datalore": { + "hide_input_from_viewers": true, + "hide_output_from_viewers": true, + "node_id": "UreOIHJpXQYfkjeHOvf1Bq", + "type": "CODE" + } + }, + "outputs": [], "source": [ "interface Observable{\n", " fun onChange(callback: (Int) -> Unit)\n", "}\n", "\n", "val observable: Observable by lazy{ TODO() }" - ], - "metadata": { - "datalore": { - "node_id": "UreOIHJpXQYfkjeHOvf1Bq", - "type": "CODE", - "hide_input_from_viewers": true, - "hide_output_from_viewers": true - } - }, - "outputs": [], - "execution_count": null + ] }, { "cell_type": "code", + "execution_count": null, + "metadata": { + "datalore": { + "hide_input_from_viewers": true, + "hide_output_from_viewers": true, + "node_id": "1in8iE7mL9m895XWUNh0Sl", + "type": "CODE" + } + }, + "outputs": [], "source": [ "val list: MutableList = mutableListOf()\n", "\n", "observable.onChange { \n", " list.add(it)\n", "}" - ], - "metadata": { - "datalore": { - "node_id": "1in8iE7mL9m895XWUNh0Sl", - "type": "CODE", - "hide_input_from_viewers": true, - "hide_output_from_viewers": true - } - }, - "outputs": [], - "execution_count": null + ] }, { - "cell_type": "markdown", - "source": "### Асинхронный х многопоточный х неконкурентный", "attachments": {}, + "cell_type": "markdown", "metadata": { "datalore": { - "node_id": "TczpOZrkCKogcT7x7XWAUT", - "type": "MD", "hide_input_from_viewers": true, - "hide_output_from_viewers": true + "hide_output_from_viewers": true, + "node_id": "TczpOZrkCKogcT7x7XWAUT", + "type": "MD" } - } + }, + "source": [ + "### Асинхронный х многопоточный х неконкурентный" + ] }, { "cell_type": "code", - "source": [], + "execution_count": null, "metadata": { "datalore": { - "node_id": "YvgL8fFuneP9USjjnsfiQj", - "type": "CODE", "hide_input_from_viewers": true, - "hide_output_from_viewers": true + "hide_output_from_viewers": true, + "node_id": "YvgL8fFuneP9USjjnsfiQj", + "type": "CODE" } }, "outputs": [], - "execution_count": null + "source": [] }, { "cell_type": "code", + "execution_count": null, + "metadata": { + "datalore": { + "hide_input_from_viewers": true, + "hide_output_from_viewers": true, + "node_id": "9NS1h5b5lJ0BEzieUt6xrr", + "type": "CODE" + } + }, + "outputs": [], "source": [ "val executor: ExecutorService = Executors.newFixedThreadPool(4)\n", "\n", @@ -311,33 +381,35 @@ " runLongTask(it)\n", " }\n", "}" - ], - "metadata": { - "datalore": { - "node_id": "9NS1h5b5lJ0BEzieUt6xrr", - "type": "CODE", - "hide_input_from_viewers": true, - "hide_output_from_viewers": true - } - }, - "outputs": [], - "execution_count": null + ] }, { - "cell_type": "markdown", - "source": "### Синхронный х многопоточный х конкурентный", "attachments": {}, + "cell_type": "markdown", "metadata": { "datalore": { - "node_id": "REBgToVc8iKIrODCWXPdZj", - "type": "MD", "hide_input_from_viewers": true, - "hide_output_from_viewers": true + "hide_output_from_viewers": true, + "node_id": "REBgToVc8iKIrODCWXPdZj", + "type": "MD" } - } + }, + "source": [ + "### Синхронный х многопоточный х конкурентный" + ] }, { "cell_type": "code", + "execution_count": null, + "metadata": { + "datalore": { + "hide_input_from_viewers": true, + "hide_output_from_viewers": true, + "node_id": "3AjItM8WtjFE9qJo9JvZFW", + "type": "CODE" + } + }, + "outputs": [], "source": [ "import kotlin.io.path.Path\n", "import kotlin.io.path.writeText\n", @@ -351,33 +423,35 @@ " .forEach { \n", " file.writeText(it)\n", " }" - ], - "metadata": { - "datalore": { - "node_id": "3AjItM8WtjFE9qJo9JvZFW", - "type": "CODE", - "hide_input_from_viewers": true, - "hide_output_from_viewers": true - } - }, - "outputs": [], - "execution_count": null + ] }, { - "cell_type": "markdown", - "source": "### Асинхронный х многопоточный х конкурентный", "attachments": {}, + "cell_type": "markdown", "metadata": { "datalore": { - "node_id": "XDCp5EjIq8v6ABeo5q2K6S", - "type": "MD", "hide_input_from_viewers": true, - "hide_output_from_viewers": true + "hide_output_from_viewers": true, + "node_id": "XDCp5EjIq8v6ABeo5q2K6S", + "type": "MD" } - } + }, + "source": [ + "### Асинхронный х многопоточный х конкурентный" + ] }, { "cell_type": "code", + "execution_count": null, + "metadata": { + "datalore": { + "hide_input_from_viewers": true, + "hide_output_from_viewers": true, + "node_id": "taOKUd5Gc9U6TuGVFZJvew", + "type": "CODE" + } + }, + "outputs": [], "source": [ "val cache = mutableMapOf\n", "\n", @@ -392,50 +466,50 @@ " consumer(result)\n", " }\n", "}" - ], + ] + }, + { + "attachments": {}, + "cell_type": "markdown", "metadata": { "datalore": { - "node_id": "taOKUd5Gc9U6TuGVFZJvew", - "type": "CODE", "hide_input_from_viewers": true, - "hide_output_from_viewers": true + "hide_output_from_viewers": true, + "node_id": "bSK4aInljsXYinbOTNSLdc", + "type": "MD" } }, - "outputs": [], - "execution_count": null - }, - { - "cell_type": "markdown", "source": [ "## Инструменты" - ], - "attachments": {}, - "metadata": { - "datalore": { - "node_id": "bSK4aInljsXYinbOTNSLdc", - "type": "MD", - "hide_input_from_viewers": true, - "hide_output_from_viewers": true - } - } + ] }, { - "cell_type": "markdown", - "source": [ - "#### Thread" - ], "attachments": {}, + "cell_type": "markdown", "metadata": { "datalore": { - "node_id": "qjWpglMy3eSM475ozgnDPA", - "type": "MD", "hide_input_from_viewers": true, - "hide_output_from_viewers": true + "hide_output_from_viewers": true, + "node_id": "qjWpglMy3eSM475ozgnDPA", + "type": "MD" } - } + }, + "source": [ + "#### Thread" + ] }, { "cell_type": "code", + "execution_count": null, + "metadata": { + "datalore": { + "hide_input_from_viewers": true, + "hide_output_from_viewers": true, + "node_id": "nEZ6jKJhVHhsjMvptTQ3rU", + "type": "CODE" + } + }, + "outputs": [], "source": [ "import kotlin.concurrent.*\n", "\n", @@ -443,49 +517,49 @@ " runLongTask()\n", "}\n", "t.join()" - ], - "metadata": { - "datalore": { - "node_id": "nEZ6jKJhVHhsjMvptTQ3rU", - "type": "CODE", - "hide_input_from_viewers": true, - "hide_output_from_viewers": true - } - }, - "outputs": [], - "execution_count": null + ] }, { "cell_type": "code", - "source": [], + "execution_count": null, "metadata": { "datalore": { - "node_id": "k35GcE4VUIMtrznZ0C3BmC", - "type": "CODE", "hide_input_from_viewers": true, - "hide_output_from_viewers": true + "hide_output_from_viewers": true, + "node_id": "k35GcE4VUIMtrznZ0C3BmC", + "type": "CODE" } }, "outputs": [], - "execution_count": null + "source": [] }, { + "attachments": {}, "cell_type": "markdown", + "metadata": { + "datalore": { + "hide_input_from_viewers": true, + "hide_output_from_viewers": true, + "node_id": "bX0GaqyAH6q7Max0Tky7DW", + "type": "MD" + } + }, "source": [ "#### Future" - ], - "attachments": {}, - "metadata": { - "datalore": { - "node_id": "bX0GaqyAH6q7Max0Tky7DW", - "type": "MD", - "hide_input_from_viewers": true, - "hide_output_from_viewers": true - } - } + ] }, { "cell_type": "code", + "execution_count": null, + "metadata": { + "datalore": { + "hide_input_from_viewers": true, + "hide_output_from_viewers": true, + "node_id": "TVhCm0JtWiYFFqWYORxuya", + "type": "CODE" + } + }, + "outputs": [], "source": [ "val future = executor.submit{\n", " runLongTask()\n", @@ -493,35 +567,35 @@ "\n", "future.get()\n", "future.cancel(true)" - ], - "metadata": { - "datalore": { - "node_id": "TVhCm0JtWiYFFqWYORxuya", - "type": "CODE", - "hide_input_from_viewers": true, - "hide_output_from_viewers": true - } - }, - "outputs": [], - "execution_count": null + ] }, { - "cell_type": "markdown", - "source": [ - "#### CompletableFuture" - ], "attachments": {}, + "cell_type": "markdown", "metadata": { "datalore": { - "node_id": "a5BY9U8m9OeZSSsS0yGRSZ", - "type": "MD", "hide_input_from_viewers": true, - "hide_output_from_viewers": true + "hide_output_from_viewers": true, + "node_id": "a5BY9U8m9OeZSSsS0yGRSZ", + "type": "MD" } - } + }, + "source": [ + "#### CompletableFuture" + ] }, { "cell_type": "code", + "execution_count": null, + "metadata": { + "datalore": { + "hide_input_from_viewers": true, + "hide_output_from_viewers": true, + "node_id": "2rchFZt5trGqzOCuU3aPvz", + "type": "CODE" + } + }, + "outputs": [], "source": [ "import java.util.concurrent.*\n", "\n", @@ -539,35 +613,35 @@ "\n", "cf2.join()\n", "cf3.join()" - ], - "metadata": { - "datalore": { - "node_id": "2rchFZt5trGqzOCuU3aPvz", - "type": "CODE", - "hide_input_from_viewers": true, - "hide_output_from_viewers": true - } - }, - "outputs": [], - "execution_count": null + ] }, { - "cell_type": "markdown", - "source": [ - "#### Lock" - ], "attachments": {}, + "cell_type": "markdown", "metadata": { "datalore": { - "node_id": "izHWuE5ZZo1hphjMmc7BHj", - "type": "MD", "hide_input_from_viewers": true, - "hide_output_from_viewers": true + "hide_output_from_viewers": true, + "node_id": "izHWuE5ZZo1hphjMmc7BHj", + "type": "MD" } - } + }, + "source": [ + "#### Lock" + ] }, { "cell_type": "code", + "execution_count": null, + "metadata": { + "datalore": { + "hide_input_from_viewers": true, + "hide_output_from_viewers": true, + "node_id": "JTXxcGnvTS36OVX6GkJfkK", + "type": "CODE" + } + }, + "outputs": [], "source": [ "import java.util.concurrent.locks.*\n", "\n", @@ -592,26 +666,20 @@ "cf5.join()\n", "\n", "list" - ], - "metadata": { - "datalore": { - "node_id": "JTXxcGnvTS36OVX6GkJfkK", - "type": "CODE", - "hide_input_from_viewers": true, - "hide_output_from_viewers": true - } - }, - "outputs": [], - "execution_count": null + ] }, { - "metadata": {}, "cell_type": "markdown", - "source": "Reactive streams" + "metadata": {}, + "source": [ + "Reactive streams" + ] }, { - "metadata": {}, "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], "source": [ "class A(val value: Int)\n", "class B(val value: Int)\n", @@ -622,18 +690,20 @@ "val combined = aFlow.zip(bFlow){ a, b->\n", " a.value + b.value\n", "}.debounce(2.seconds)" - ], - "outputs": [], - "execution_count": null + ] }, { - "metadata": {}, "cell_type": "markdown", - "source": "Actor" + "metadata": {}, + "source": [ + "Actor" + ] }, { - "metadata": {}, "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], "source": [ "sealed class Event {\n", " class Open(val transactionId: Int): Event()\n", @@ -662,42 +732,50 @@ " }\n", "\n", "}" - ], - "outputs": [], - "execution_count": null + ] }, { + "attachments": {}, "cell_type": "markdown", + "metadata": { + "datalore": { + "hide_input_from_viewers": true, + "hide_output_from_viewers": true, + "node_id": "YDqoXAD2XEED9MSPRuMMzL", + "type": "MD" + } + }, "source": [ "## Сорта асинхронности" - ], - "attachments": {}, - "metadata": { - "datalore": { - "node_id": "YDqoXAD2XEED9MSPRuMMzL", - "type": "MD", - "hide_input_from_viewers": true, - "hide_output_from_viewers": true - } - } + ] }, { - "cell_type": "markdown", - "source": [ - "#### Callback" - ], "attachments": {}, + "cell_type": "markdown", "metadata": { "datalore": { - "node_id": "kb1ccsTMIZX3aacLF6gPrJ", - "type": "MD", "hide_input_from_viewers": true, - "hide_output_from_viewers": true + "hide_output_from_viewers": true, + "node_id": "kb1ccsTMIZX3aacLF6gPrJ", + "type": "MD" } - } + }, + "source": [ + "#### Callback" + ] }, { "cell_type": "code", + "execution_count": null, + "metadata": { + "datalore": { + "hide_input_from_viewers": true, + "hide_output_from_viewers": true, + "node_id": "jZrkouEiPJnMCCpxTFlgVP", + "type": "CODE" + } + }, + "outputs": [], "source": [ "import java.net.*\n", "import java.net.http.*\n", @@ -729,61 +807,60 @@ " }\n", " .thenAccept{ println(it.toList()) }\n", " .join()" - ], - "metadata": { - "datalore": { - "node_id": "jZrkouEiPJnMCCpxTFlgVP", - "type": "CODE", - "hide_input_from_viewers": true, - "hide_output_from_viewers": true - } - }, - "outputs": [], - "execution_count": null + ] }, { - "cell_type": "markdown", - "source": [ - "#### Реактивные потоки" - ], "attachments": {}, + "cell_type": "markdown", "metadata": { "datalore": { - "node_id": "7zwURNaHnaTgOTAZumDDcd", - "type": "MD", "hide_input_from_viewers": true, - "hide_output_from_viewers": true + "hide_output_from_viewers": true, + "node_id": "7zwURNaHnaTgOTAZumDDcd", + "type": "MD" } - } + }, + "source": [ + "#### Реактивные потоки" + ] }, { "cell_type": "code", - "source": [], + "execution_count": null, "metadata": { "datalore": { - "node_id": "lPXl5Gkvu7CuhlhUV7YJab", - "type": "CODE", "hide_input_from_viewers": true, - "hide_output_from_viewers": true + "hide_output_from_viewers": true, + "node_id": "lPXl5Gkvu7CuhlhUV7YJab", + "type": "CODE" } }, "outputs": [], - "execution_count": null + "source": [] } ], "metadata": { + "datalore": { + "base_environment": "default", + "computation_mode": "JUPYTER", + "package_manager": "pip", + "packages": [], + "report_row_ids": [], + "version": 3 + }, "kernelspec": { "display_name": "Kotlin", "language": "kotlin", "name": "kotlin" }, - "datalore": { - "computation_mode": "JUPYTER", - "package_manager": "pip", - "base_environment": "default", - "packages": [], - "report_row_ids": [], - "version": 3 + "language_info": { + "codemirror_mode": "text/x-kotlin", + "file_extension": ".kt", + "mimetype": "text/x-kotlin", + "name": "kotlin", + "nbconvert_exporter": "", + "pygments_lexer": "kotlin", + "version": "1.9.23" } }, "nbformat": 4, diff --git a/src/main/kotlin/javaHttpClient.kt b/src/main/kotlin/javaHttpClient.kt new file mode 100644 index 0000000..f1b5446 --- /dev/null +++ b/src/main/kotlin/javaHttpClient.kt @@ -0,0 +1,34 @@ +import java.net.* +import java.net.http.* +import java.net.http.HttpResponse.* + +fun main() { + val client: HttpClient = HttpClient.newHttpClient() + + val request : HttpRequest = HttpRequest.newBuilder() + .uri(URI.create("https://sciprog.center")) + .GET().build() + + val regex = """href=\"(.+\.png)\"""".toRegex() + + client.sendAsync(request, BodyHandlers.ofString()) + .thenApply{ it.body() } + .thenApply{ + val resources = regex.findAll(it).map{it.groupValues[1]} + + resources.map { resourceName-> + println("Resource processing for $resourceName started") + val resourceRequest : HttpRequest = HttpRequest.newBuilder() + .uri(URI.create("https://sciprog.center$resourceName")) + .GET().build() + client.sendAsync(resourceRequest, BodyHandlers.ofByteArray()).thenAccept{ resourceResponse -> + val bodyBytes = resourceResponse.body() + //do something with the body + println("The resource with name $resourceName has ${bodyBytes.size} bytes") + } + } + resources + } + .thenAccept{ println(it.toList()) } + .join() +} \ No newline at end of file diff --git a/src/main/kotlin/ktorClient.kt b/src/main/kotlin/ktorClient.kt new file mode 100644 index 0000000..8eeaf4b --- /dev/null +++ b/src/main/kotlin/ktorClient.kt @@ -0,0 +1,26 @@ +import io.ktor.client.HttpClient +import io.ktor.client.call.body +import io.ktor.client.engine.cio.CIO +import io.ktor.client.request.get +import io.ktor.client.statement.bodyAsBytes +import io.ktor.client.statement.bodyAsText +import kotlinx.coroutines.coroutineScope +import kotlinx.coroutines.launch + +suspend fun main() = coroutineScope { + val client = HttpClient(CIO) + + val regex = """href=\"(.+\.png)\"""".toRegex() + + val initialResponse = client.get("https://sciprog.center") + val resources = regex.findAll(initialResponse.bodyAsText()).map { it.groupValues[1] } + resources.forEach { resourceName -> + launch { + println("Resource processing for $resourceName started") + val resourceResponse = client.get("https://sciprog.center$resourceName") + val bodyBytes: ByteArray = resourceResponse.bodyAsBytes() + //do something with the body + println("The resource with name $resourceName has ${bodyBytes.size} bytes") + } + } +} \ No newline at end of file