如何计算在 Kotlin 中创建的线程数?
Posted
技术标签:
【中文标题】如何计算在 Kotlin 中创建的线程数?【英文标题】:How to count number of threads created in Kotlin? 【发布时间】:2022-01-17 19:06:44 【问题描述】:我有套接字编程,我正在为每个客户端创建新线程并使用具有多线程的应用程序,如下所示。
fun ClientHandler()
try
var count = 0
var serverSocket = ServerSocket(5000)
while (true)
val socket: Socket = serverSocket!!.accept()
count++
socket.reuseAddress = true
_log("$socket.remoteSocketAddress Client Connected. Connected client count is $count")
Thread(ServerThread(socket)).start() //Here i'm creating Threads.
catch (e: ConnectException)
_logEx("UnExpected connection error", e)
catch (ex: IOException)
_logEx("ClientHandler", ex)
catch (e: Exception)
_logEx("ServerSocketHandler ", e)
internal inner class ServerThread(socket: Socket) : Runnable
private val client: Socket = socket
private val reader: Scanner = Scanner(client.getInputStream())
private val writer = PrintStream(client.getOutputStream())
private var receiveString: String = ""
override fun run()
try
while(reader.hasNextLine())
receiveString = reader.nextLine() ?: ""
_log("Message received: " + receiveString)
if (receiveString != null && receiveString != "")
checkMessage(receiveString, writer,client)
catch (ex: Exception)
_log("Cannot read the message. Please try again")
//_log(ex.localizedMessage)
val spReq = Error()
spReq.sendErrorMsg(writer, "Cannot read the message. Please try again",client)
更新 我也添加了 ServerThread 代码部分。
如何关注他们并获得他们的状态?
【问题讨论】:
跟踪他们是什么意思?定期检查他们的状态?检查他们是否被杀? @DiegoMarcher 是的,我想获得他们的状态?(活着、睡觉或被杀)以及他们有多少? 然后我只需创建一个包含所有已创建线程的 Map 以供进一步参考 @DiegoMarcher 你能举个例子吗?我是 Kotlin 的新手 我会把它作为答案发布 【参考方案1】:您可以通过 Map 来跟踪线程及其状态以供进一步参考
fun ClientHandler()
try
var count = 0
val serverSocket = ServerSocket(5000)
while (true)
val socket: Socket = serverSocket.accept()
count++
socket.reuseAddress = true
threads["thread name here"] = ServerThread(socket)
catch (e: ConnectException)
e.printStackTrace()
catch (e: IOException)
e.printStackTrace()
catch (e: Exception)
e.printStackTrace()
fun stopServerThread(threadName: String)
threads[threadName]?.interrupt()
internal inner class ServerThread(private val client: Socket) : Thread()
private val reader = Scanner(client.getInputStream())
private val writer = PrintStream(client.getOutputStream())
private var receiveString: String = ""
init
start()
override fun run()
try
while(reader.hasNextLine())
if (isInterrupted)
break
receiveString = reader.nextLine() ?: ""
if (receiveString != "")
checkMessage(receiveString, writer, client)
catch (ex: Exception)
_log("Cannot read the message. Please try again")
//_log(ex.localizedMessage)
val spReq = Error()
spReq.sendErrorMsg(writer, "Cannot read the message. Please try again",client)
【讨论】:
好的,但是 checkSocketThred 应该是可运行的,对吧?你写的是正常的功能。 看你想在哪里调用它,你可以在一个单独的线程中进行这个检查,你可以让线程进行自检。我不确定你想在这里实现什么...... 我正在尝试创建线程,在该线程中我正在打开套接字接收和发送消息,然后关闭套接字......但线程保持打开并在应用程序上休眠(我在 Profiler android Studio 上观看) 我担心会炸毁内存.... 能否贴出原题中的ServerThread代码内容? 发布的ServerThread以上是关于如何计算在 Kotlin 中创建的线程数?的主要内容,如果未能解决你的问题,请参考以下文章
有啥方法可以为 CuPy 计算设置线程数、块数和网格数?如何?
释放在 WPF Application.OnStartUp() 中创建的命名互斥锁:哪个线程拥有它?