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

时间:2012-01-09 04:49:54

标签: java blackberry

ImageThread it = new ImageThread(this.imageURL,this);

        Thread t = new Thread(it);
        t.start();

我是线程新手,不得不在我的字段中实现上面的内容来加载图片,因为它减慢了UI线程。

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

我应该对我认为有责任的线程做些什么?我不希望我的应用程序被一个线程队列陷入困境。我怎么说,取消屏幕更改下载?

我在Java中发布这个问题,因为我觉得这个过程是一样的。

2 个答案:

答案 0 :(得分:0)

由于@Rupak建议您制作方法(例如使用isDisplayed()):

boolean isScreenOnTop()

并将其传递给Thread(更好地通过界面StopCondition.shouldStop())。并修改你下载算法到下一个:

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

if (!topCondition.shouldStop()) {
   notifyDataDownloaded();
}

答案 1 :(得分:0)

您可以通过在扩展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. how-to-abort-a-thread-in-a-fast-and-clean-way-in-java

  2. How can we kill the running thread in java?

  3. How to Stop a Thread or a Task
  4. 希望这些对您有所帮助。