黑莓套接字连接和线程
我正在开发一个有 2 个按钮的程序:开始和结束。当我单击“开始”时,它将开始每 30 秒获取一次位置信息。并使用套接字连接将它们发送到服务器。当我单击“结束”按钮时,它将停止所有工作。 我可以在不使用线程的情况下做到这一点吗?如果我需要使用线程,那么实现它的最佳方法是什么。我应该使用 LocationListener 监听 LocationProvider 以每 30 秒获取位置并在线程中发送它们,还是应该创建连接线程并在每个线程中获取位置并发送。
I am developing a program which have 2 buttons : Start and End. When i click Start it will start to get Locations in every 30sec. and send them to a server using socket connection. When i click the End button it will stop all works.
Can i do this without using thread? If i need to use threads what is the best way to implement it. Should i listen LocationProvider with LocationListener to get locations every 30sec and send them in a thread or should i create connection thread and get location in every and send.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
不幸的是,这是一个部分答案(我很难理解你问题的其余部分):
这是不可能的。如果您开始在主 UI 线程上执行此操作,那么您的应用程序 UI 将必须等到所有操作完成(网络是一项耗时的工作)。所以唯一的方法是在后台线程上执行此操作。
Unfortunatelly this is a partial answer (it's difficult for me to understand the rest of your question):
This is impossible. If you start doing this on the main UI thread, then your app UI will have to wait untill all is done (networking is a time-consuming undertaking). So the only way is to do this on a background thread.
如果您在 UI 主线程中建立连接,您的应用程序将变得无响应,直到连接终止,这是错误的!!!您必须为建立的每个连接创建一个线程,强烈建议这样做。
您必须创建一个每次休眠 30 秒的线程,然后执行您所说的工作。
你的工作应该在线程中完成,不要使用主线程来做这些事情
If you make a connection in the UI main thread, you application will become unresponsive until the connection is terminated, which is wrong!!! You have to create a thread for each connection you establish, this is strongly recommended.
You will have to create a thread that will sleep for 30 seconds each time and then do the work that you said.
Your work should be done in threads, do not use the main thread for these things
除了主 UI 线程 (EDT) 之外,您还必须使用另一个线程来进行网络事务。
您不能使用主线程进行网络事务,因为这是一个耗时的操作,会使主线程无响应,并且您的应用程序最终将崩溃。
You must use another thread besides the main UI thread (EDT) for network transactions.
You can not use the main thread for network transactions because this is a time consuming action that will make the main thread unresponsive and your application will eventually crash.