Kotlin - How to ping multiple HTTP endpoints by run Multiple Threads Concurrently?
Hello everyone, today we are going to learn how to ping multiple HTTP endpoints by running multiple threads concurrently in Kotlin. I am using ExecutorService Approach to achieve the goal. What is the ExecutorService? The ExecutorService is the interface that sanctions us to execute tasks on threads asynchronously. The ExecutorService avails in maintaining a pool of threads and assigns them tasks. It withal provides the facility to queue up tasks until there is a free thread available if the number of tasks is more than the threads available. Example: import kotlin. Throws import kotlin.jvm. JvmStatic import java.util.concurrent. ExecutorService import java.util.concurrent. Executors import java.lang. Runnable import KnfPingStatusWithExecutorService . MyRunnable import java.lang. Exception import java.net. HttpURLConnection import java.net. URL /** * @author knf */ object KnfPingStatusWithExecutorService { private const val MYTHREADS = 10 @ Throws ( Exception :: class )