【发布时间】: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 吗?我建议您使用适当的标记编辑您的问题(花括号按钮 {} 允许您发布代码)并确保发布所有相关的代码片段。看起来你的代码逻辑永远不会从你正在使用的任何结构中拉出挂起的线程,但很难判断那里有什么。