开发者

How to stop a Thread if another one identical starts

Every time the user presses a button, the following code is executed.

new Thread() {
    @Override
    public void run() {
        aA = new albumArt(foo.getField("albumArt"),albumArt.GLOSSY_1, albumart,getBaseContext());
        runOnUiThread(new Runnable() {
            public void run() {
                    aA.placeAlbumArt();
            }
        });
    }
}.start();

There's a problem with this:

For example: User presses the button => Image A starts to get downloaded. User presses the butt开发者_JS百科on (before Image A has finished) => Image B starts to get downloaded.

Image B finishes (before Image A) to get downloaded and then it's shown (with aA.placeAlbumArt()). Image A finishes and replaces Image B.

How can I avoid that Image A replaces Image B if it finishes later?

I mean, is there any way I can cancel a Thread (Image A) if another one (Image B) gets started before the first one finishes?

That would also help not to queue a lot of clicks and doing the whole process uselessly. I mean, if the user presses the button before Image A has finished, there's no point in finishing to process Image A. If the user presses the button 5 times before Image A finishes, there's no point in downloading Image A, B, C and D. It should only download E (the fifth click).


If you were to use an AsyncTask then you could cancel it. You should be able to swap your Thread out for an AsyncTask without too much trouble.


i think you can disable the click on the button , until the image A is downloaded , and then , enable the clic again in order to let the user download the second image B , and so on .

and while downloading the image A or B or ... etc, you can display a sweet msg to tell the user to wait , example : waiting .... etc


Simple solution: Put your thread in a static variable of type Thread. Interrupt it if it's not null before creating one.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜