【发布时间】:2012-06-19 20:25:09
【问题描述】:
我正在为安卓手机编写一个简单的应用程序,通过套接字连接与 PC 进行通信。
手机可以随时写或接收消息,计算机也可以。 到目前为止我使用的解决方案是这样的:
Create a thread and call READ in it.
Run an infinte loop.
Check if thread has finished,
If so grab the READ and process,
Then start a new thread also calling read.
Check to see if another object working in another thread wants to write,
If so grab and write.
具体来说,我使用 Android API 中的 AsyncTask 来运行线程。
一切正常,但我想知道为每个 READ 创建一个新线程是否性能太高和/或编码不好,如果是这样,我如何重用相同的线程以具有相同的行为。
或者,有没有更好的方法来处理这种情况?
提前感谢您的任何建议!
【问题讨论】:
-
您认为有多少连接是同时的?
-
在这个应用程序中,只有一个客户端。我只是使用线程,这样当应用程序可能想要进行 WRITE 时,READ 不会阻塞。
标签: java android multithreading sockets android-asynctask