Welcome!

Welcome to the official BlackBerry Support Community Forums.

This is your resource to discuss support topics with your peers, and learn from each other.

inside custom component

Java Development

Reply
Highlighted
New Developer
Posts: 134
Registered: ‎01-11-2011
My Device: curve

Thread problem

Hi all

 

I have 25 bitmapfield on a horizontalFieldManger which is scrollable.And on scroll i need to download a image for each bitmapfield.I am doing it via starting a new thread on each scroll.But on scroll continusoly many thread starts together.I want to make last thread workable only and cancel all other.What should i do.

 

 

Thread th = new Thread(new Runnable() {

public void run() {
iconImage = ConnectivityController.getImages(ch
.getChan_preview_img(), true, widthForPreview,
heightForPreview);

 

//above line is to get the image from server.


UiApplication.getUiApplication().cancelInvokeLater(neweventId);
neweventId = UiApplication.getUiApplication().invokeLater(
new Runnable() {
public void run() {
NameLabel.setText(ch.getname());
BitmapField.setBitmap(iconImage);

}
}, 200, false);
}
});

Developer
Posts: 16,992
Registered: ‎07-29-2008
My Device: Z10 LE, Z30, Passport
My Carrier: O2 Germany

Re: Thread problem

you could use a queue to avoid spawning new threads.
canceling threads is difficult if they are within a blocking task.
----------------------------------------------------------
feel free to press the like button on the right side to thank the user that helped you.
please mark posts as solved if you found a solution.
@SimonHain on twitter
Developer
Posts: 410
Registered: ‎06-03-2010
My Device: Z10 Red
My Carrier: Free

Re: Thread problem

I suggest like simon to use a Thread Pool instead of stopping a thread.

 

If you still need it, you must implements yourself the stop behaviour to return from your Thead.

Do it inside your function ConnectivityController.getImages(). Set a flag to true when you want it to stop, and check the flag when you are reading the inputStream of the image data, byte[] by byte[]. If the flag is true, just stop the download and return.

Developer
Posts: 19,636
Registered: ‎07-14-2008
My Device: Not Specified

Re: Thread problem

Rather than worrying about a Thread pool, I would do what Simon suggested.  Fleshing that out a bit, implement a single Thread with a queue, and an 'addToQueue' method.  In the addToQueue() method, check if the Thread is there and isAlive, and if not, start it.  The Thread can search its queue and stop when there is nothing to do.  Then you won't have processing running all the time. 

Developer
Posts: 180
Registered: ‎12-09-2011
My Device: 9360, BB10
My Carrier: Aircel

Re: Thread problem

Thanks peter i am also facing this problem and will apply that one.