黑莓:在推送另一个屏幕时处理启动的线程


ImageThread it = new ImageThread(this.imageURL,this);
        Thread t = new Thread(it);
        t.start();

我是线程新手,必须在加载图像的字段中实现上述内容,因为它减慢了 UI 线程的速度。

无论我的线程正在下载图像还是某些 json 内容,即使用户已将新的主屏幕推送到 uiapplication 上,它们似乎仍在下载。如果用户进入一个屏幕,然后快速连续访问另一个屏幕,则这种连续加载可能会有问题。结果,它们所在的最后一个屏幕仅在其他屏幕完成后完成其线程。

我应该如何处理被视为负责任的线程?我不希望我的应用程序因线程队列而陷入困境。例如,如何在屏幕上取消下载更改?

我在 Java 中发布这个问题,因为我认为过程是相同的。

您可以通过在

类中保留扩展 Thread 的公共close()方法来强制关闭线程:

private class MyConnectionThread extends Thread {
        /** determines whether the thread is runnuing or not. */
        private boolean alive = false;
        private HttpConnection hconn = null; // or whatever connection method you want to use ( SocketConnection etc.)
        private InputStream inputStream = null; // or DataInputStream etc...
        public MyConnectionThread() {
            alive = false;
            // ...
            // ...
        }
        public void run() {
            alive = true;
            try {
                String connection_parameter = ";deviceside=false"; // [For BlackBerry: ] this parameter is for connection using MDS, need to add different parameters for different connection methods.
                hconn = (HttpConnection) Connector.open("http://your_url.com"+connection_parameter);
                int response = hconn.getResponseCode();
                if (response == HttpConnection.HTTP_OK) {
                    inputStream = hconn.openInputStream();
                    // process the result here by reading the inputStream ...
                    // ...
                    // ...
                }
                inputStream.close();
                hconn.close();
            }catch(Exception excp) {
                // Handle Exceptions here...
            }catch (Throwable e) {
               // Exception in reading inputStream
            }finally{
                alive = false;
                this.interrupt();
            }
        }
        /**
         * Forces the connection to close anytime.
         */
        public void closeConnection() {
            alive = false;
            try {
                if (inputStream != null) {
                    inputStream.close();
                }
                inputStream = null;
                if (hconn != null) {
                    hconn.close();
                }
                hconn = null;
                this.interrupt();
            } catch (Exception excp) {
                // Handle Exception here...
                System.out.println("Exception in closing HttpConnection: " + excp.toString());
            }
        }
    }

现在,每当您导航到另一个屏幕时,您只需要调用 MyConnectionThread.closeConnection() 方法来强制关闭此线程。

另请参阅:

  1. 如何中止线程在 Java 中的快速和清洁方式

  2. 我们如何杀死 java 中的运行线程?

  3. 如何停止线程或任务

希望这些对您有所帮助。

正如@Rupak建议你制作方法(例如使用 isDisplayed()(:

boolean isScreenOnTop()

并将其传递给Thread(最好通过接口StopCondition.shouldStop()(。并将您的下载算法修改为下一步:

while(moreDataAvailable() && !topCondition.shouldStop()) {
   loadNextDataChunk();
}
if (!topCondition.shouldStop()) {
   notifyDataDownloaded();
}

最新更新