杀死一个的JavaME 1.2线程的最佳实践? [英] Best Practice for killing a JavaME 1.2 thread?

查看:132
本文介绍了杀死一个的JavaME 1.2线程的最佳实践?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

问:我很想知道杀死正在运行的后台线程一个长期运行的最佳实践(让我们称之为这个线程的WorkerThread )。

Question: I'm interested to know the best practice for killing a long standing operation that is running as a background thread (lets call this thread WorkerThread) in Java 1.2.

具体来说,我正在开发的黑莓设备,由此我提出一个HTTP连接的应用程序。大图:如果转发到后台线程(的WorkerThread )的URL请求,线程发出请求,并使用一个回调返回结果

Specifically, I'm developing an application for Blackberry devices whereby I make a HTTP connection. Big picture: a URL request if forwarded to a background thread (WorkerThread), the thread makes the request and returns the result using a call back.

现在存在,其中在连接时,数据连接存在的情况下的但随后无论出于何种原因(通过隧道驱动器)进行连接不再存在。由于在黑莓设计结构的限制,实际的连接将挂起作为超时被固定为2分钟。其结果是,有一个迫切需要杀掉一直挂的时间比较(15秒),长周期的连接。

Now there exists a situation where at connection time, a data connection exists but then for whatever reason (drives through a tunnel) that connection no longer exists. Due to a limitation in Blackberry's design architecture, that actual connection will hang as the time out is fixed to be 2 minutes. As a result, there's a crucial need to kill a connection that has been hanging for a relatively (15 seconds) long period of time.

现在我目前的解决方案是运行的WorkerThread 另一个线程内(可以称之为新的线程 MonitorThead )。 MonitorThread 启动的WorkerThread ,休眠1000毫秒,然后定期检查,如果的WorkerThread 仍然活着。如果15秒后的WorkerThread还活着,MonitorThread提出的WorkerThread睡觉并退出。这真的是最好的方法呢?

Right now my current solution is to run WorkerThread inside another thread (lets call this new thread MonitorThead). MonitorThread starts WorkerThread, sleeps for 1000ms and then routinely checks if WorkerThread is still alive. If after 15 seconds WorkerThread is still alive, MonitorThread puts WorkerThread to sleep and exits. Is this really the best possible approach?

在总结,下面是与问题有关的核心问题和关键约束。干杯!

In summary, below is the core question and key restraints associated with the question. Cheers!

我如何成功击杀一个java后台线程卡在具体的操作?

How do I successful kill a java background thread that is stuck in a specific operation?


  • 其操作暂停的无控制
    并检查线程的请求的状态

  • 针对黑莓的
    执行的Java ME 1.2和其
    线程API 所以没有明确的
    杀()方式

  • 最关心的最佳实践,以及如何
    最安全地杀死一个线程持有

  • No control of having operation pause and check the threads requested state
  • Specific to Blackberry's implementation of Java ME 1.2 and its Thread API so no explicit kill() method
  • Most concerned about the best practice and how to most safely kill a holding thread.


跟进/修改结果
尼尔·科菲建议我只是坚持的连接对象的引用,而是叫关闭()的对象。我目前正在研究这个...

Follow Up/Edit
Neil Coffey recommended that I simply hold a reference to the connection object and instead call close() on that object. I am currently looking into this...

推荐答案

如何杀死一个线程是一个很难回答的问题。没有任何万无一失的方法能够停止或中断线程。但是,如果你把你目前的架构和超时后,只需关闭流(而不是<一个href=\"http://www.blackberry.com/developers/docs/4.2api/javax/microedition/io/Connection.html#close%28%29\"相对=nofollow>连接),这应该会导致卡在I / O的线程上发生的I / O异常。如果它不会导致一个IOException,那么它至少应该引起读或写EOF返回。

How to kill a Thread is a difficult question. There is no guaranteed way to be able to stop or interrupt a Thread. However, if you take your current architecture and upon timeout, just close the stream (not the Connection), that should cause an I/O Exception to occur on the thread that is stuck in I/O. If it doesn't cause an IOException, then it should at least cause the read or write to return with EOF.

注意,关闭连接没有帮助,因为JavaDoc的说:

Note that closing the Connection doesn't help, as the JavaDoc says:

任何打开的流会导致连接举行开放至
  他们自己关闭。

Any open streams will cause the connection to be held open until they themselves are closed.

您必须关闭从连接产生的数据流。

You have to close the stream that was derived from the Connection.

这篇关于杀死一个的JavaME 1.2线程的最佳实践?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆