简体   繁体   English

BlackBerry:按下另一个屏幕时处理启动线程

[英]BlackBerry: Handling started threads when another screen has been pushed

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

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

I'm new to threads and had to implement the above in my field that loads an image, because it slowed down the UI thread. 我是线程的新手,必须在加载图像的字段中实现上述功能,因为这会减慢UI线程的速度。

Whether my threads are downloading an image or some json content, they appear to still be downloading even though the user has pushed a new mainscreen onto the uiapplication. 无论我的线程正在下载图像还是一些json内容,即使用户已将新的主屏幕推送到ui应用程序上,它们似乎仍在下载。 This continuous loading could be a problem if a user enters a screen and then accesses another in quick succession. 如果用户进入屏幕然后快速连续访问另一个屏幕,则这种连续加载可能会成为问题。 As a result, the last screen they are on only finishes its thread when the others are done. 结果,它们所在的最后一个屏幕仅在其他屏幕完成后才完成其线程。

What am I supposed to do with my threads that would be deemed responsible? 我应该如何处理被认为是负责任的线程? I don't want my app to be bogged down by a queue of threads. 我不希望我的应用程序因线程队列而陷入困境。 How do I, say, cancel downloads on screen change? 我要如何取消屏幕更改下载?

I'm posting this question in Java since I think the process is the same. 我在Java中发布此问题,因为我认为过程是相同的。

As @Rupak suggested you make method (using isDisplayed() for example): 正如@Rupak建议的那样,您可以制作方法(例如,使用isDisplayed() ):

boolean isScreenOnTop()

And pass it to the Thread (better over interface StopCondition.shouldStop() ). 并将其传递给Thread (最好通过接口StopCondition.shouldStop() )。 And modify you downloading algorithm to next: 然后将您的下载算法修改为:

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

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

You can force your threads to close by keeping a public close() method inside your class that extends Thread: 您可以通过在扩展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());
            }
        }
    }

Now whenever you navigate to another screen you just need to call the MyConnectionThread.closeConnection() method to force-close this Thread. 现在,每当导航到另一个屏幕时,您只需要调用MyConnectionThread.closeConnection()方法即可强制关闭此线程。

See Also: 也可以看看:

  1. how-to-abort-a-thread-in-a-fast-and-clean-way-in-java 如何在Java中快速干净地中止线程

  2. How can we kill the running thread in java? 我们如何杀死Java中正在运行的线程?

  3. How to Stop a Thread or a Task 如何停止线程或任务

Hope these will be helpful for you. 希望这些对您有所帮助。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 孩子的数据被推送后如何获取? - How to get a child's data when it has been pushed? Junit测试已启动的正确线程数 - Junit test the correct number of threads has started AppiumServerHasNotBeenStartedLocallyException: 本地 appium 服务器尚未启动 - AppiumServerHasNotBeenStartedLocallyException: The local appium server has not been started 获取IgniteCheckedException:在单个节点上启用Persistence时,默认Ignite实例已经启动异常 - Getting IgniteCheckedException: Default Ignite instance has already been started exception when enabling Persistence on single Node Java:尚未加载屏幕 - Java: Screen has not been loaded 如何获取Firebase中推送给孩子的最新数据? - How to retrieve the latest data that has been pushed to a child in Firebase? 澄清何时以及在何处启动和停止新线程 - Clarify when and where new threads are started and stopped 在另一个方法被调用后调用方法时是否存在nullpointerexception? - nullpointerexception when calling method after another method has been called? 如何判断其他课程何时更新? - How can I tell when another class has been updated? 避免线程已启动异常(Android) - Avoid Thread has already been started exception (Android)
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM