在多线程中显示来自Web网址的图像

时间:2011-09-12 04:47:59

标签: multithreading blackberry

我有52张图片来自网址。我想在多线程过程中显示这些图片。

任何人都可以指导我怎么可能?????

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

    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);
                        }
                    });

        } 

}

提前致谢。

2 个答案:

答案 0 :(得分:1)

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

答案 1 :(得分:0)

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