【问题标题】:Display images from web url in multi threading在多线程中显示来自 web url 的图像
【发布时间】:2011-11-18 20:22:18
【问题描述】:

我有 52 张来自 Web URL 的图像。我想在多线程进程中显示这些图像。

谁能指导我怎么可能????

我实现了下面的代码,但它不起作用。

    for (int i = 0; i <total_data; i++) {

        GetBitmapClass getBitmapClass=new GetBitmapClass(i, image_path[i]);

        if(Thread.activeCount()>14)
        {
            //System.out.println("Size "+pending_thread.size());
            pending_thread.addElement(getBitmapClass);

        }else
        {

            getBitmapClass.start();
        }

}


        //Thread class




        class GetBitmapClass extends Thread
        {
            int index;
            String url;

            public GetBitmapClass(int index,String url)
            {
                this.index=index;
                this.url=url;

            }

            public void run() {
               // TODO Auto-generated method stub



                StreamConnection stream = null;
                InputStream in = null;


                if(Thread.activeCount()>14)
                {
                    try {
                        Thread.sleep(1000);
                    } catch (InterruptedException e) {
                        // TODO Auto-generated catch block
                        e.printStackTrace();
                    }
                }




                System.out.println("Thread "+index+" Started \n count= "+Thread.activeCount());

                try {
                    //stream = (StreamConnection) Connector.open(url+";deviceside=true;");
                    stream = (StreamConnection) Connector.open(url+";interface=wifi");
                    in = stream.openInputStream();
                } 
                catch (Exception e) {

                }

                byte[] data = new byte[1024];
                try {
                    DataBuffer db = new DataBuffer();
                    int chunk = 0;
                    while (-1 != (chunk = in.read(data))) {
                        db.write(data, 0, chunk);
                    }
                    in.close();

                    data = db.getArray();
                } catch (Exception e) {
                }

                EncodedImage jpegPic = EncodedImage.createEncodedImage(data, 0,
                        data.length);
                        Bitmap bm = jpegPic.getBitmap();

                        bmp[index]=bm;


                    UiApplication.getUiApplication().invokeLater (new Runnable() {
                        public void run() 
                        {   

                            bitmapFields[index].setBitmap(bmp[index]);
                            gridFieldManager.add(bitmapFields[index]);

                            if((pending_thread.size()>0))
                                {
                                    GetBitmapClass thread1=(GetBitmapClass)pending_thread.elementAt(0);
                                    try
                                    {
                                        thread1.start();
                                        pending_thread.removeElementAt(0);
                                    }
                                    catch (Exception e) {
                                        // TODO: handle exception
                                        System.out.print("Thread Not Started");
                                    }

                                    System.out.println("Size Reduce"+pending_thread.size());
                                }
                            System.out.print("Thread Completed"+index);
                        }
                    });

        } 

}

提前致谢。

【问题讨论】:

  • 这里发生了什么.. 似乎它应该工作。检查你的防火墙。然后给我正确的信息它是如何表现的。
  • 此代码在黑莓模拟器中正常工作,但在黑莓设备中最多加载 32 张图像。我不知道设备中究竟发生了什么。所以给我一些建议。
  • 检查这可能是图像大小的问题。如果图像大小很大,那么它可能会给您带来内存问题。结果,它会抛出一些内存异常。你得到任何这样的异常运行时.. ??
  • No Neel 我没有得到任何类型的运行时异常。
  • 我花了一些时间用适当的代码标记重新格式化您的问题。我认为您没有准确地发布您的代码段。例如:你真的在 for 循环中声明 GetBitmapClass 吗?我建议您使用适当的标记编辑您的问题(花括号按钮 {} 允许您发布代码)并确保发布所有相关的代码片段。看起来你的代码逻辑永远不会从你正在使用的任何结构中拉出挂起的线程,但很难判断那里有什么。

标签: multithreading blackberry


【解决方案1】:

我忘了关闭流连接。它需要关闭流连接。

【讨论】:

    【解决方案2】:

    尝试同时运行大量线程对 BB 来说是个坏主意。这不是桌面。通常你应该避免运行多个繁重的后台线程,否则你可能会降低你的主 UI 线程到用户将停止使用你的应用程序并称之为“缓慢”的水平。你的线程很重——它们调整图像大小、进行网络连接、操作位图。为了解决这个问题,您需要创建一个Runnable 任务列表并在唯一的后台线程上执行它们。根据我的经验,在 BB 上按顺序执行任务比尝试一次运行所有任务更有效。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2014-06-04
      • 2017-04-20
      • 2018-12-13
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多