取消“一会儿”的问题Android 中的循环
首先,对于这个不太具有描述性的标题感到抱歉。
我有以下代码,它无法正常工作(或如我想要的那样)。
即使我调用“Cancel()”,但时间仍在继续......
private boolean cancelled;
private synchronized Bitmap renderBitmap(PatchInputStream pis){
File file = new File(Environment.getExternalStorageDirectory()+url);
FileOutputStream fos=new FileOutputStream(file);
byte buf[]=new byte[2000];
int len;
while((len=pis.read(buf))>0 && !isCancelled() ){
fos.write(buf,0,len);
}
fos.close();
if (isCancelled()){
cancelled=false;
return null;
}
Bitmap bm = saveBitmapToFile(file);
return bm;
}
public boolean isCancelled(){
return cancelled;
}
public void Cancel(){
cancelled=true;
}
这段代码有什么问题?我错过了什么吗?
First of all, sorry for the not-very-descriptive title.
I've got the following code that it's not working properly (or as I'd wanted).
Even though I call "Cancel()", the while goes on...
private boolean cancelled;
private synchronized Bitmap renderBitmap(PatchInputStream pis){
File file = new File(Environment.getExternalStorageDirectory()+url);
FileOutputStream fos=new FileOutputStream(file);
byte buf[]=new byte[2000];
int len;
while((len=pis.read(buf))>0 && !isCancelled() ){
fos.write(buf,0,len);
}
fos.close();
if (isCancelled()){
cancelled=false;
return null;
}
Bitmap bm = saveBitmapToFile(file);
return bm;
}
public boolean isCancelled(){
return cancelled;
}
public void Cancel(){
cancelled=true;
}
What's wrong with this code? Am I missing something?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
至少代码有一个并发错误,可以通过设置
cancelled
volatile
来解决。否则,无法保证其他线程看到新值。(显然情况并非如此。)
另一种可能性是未调用 cancel() 方法。
(显然情况并非如此。)
还有一种可能是在不同的对象实例上调用 cancel() 方法。尝试将 System.out.println(System.identityHashCode(this)) 添加到 cancel() 方法和 while 循环,看看它们是否打印相同的值。
At least the code has a concurrency bug which can be solved by making
cancelled
volatile
. Otherwise the other thread is not guaranteed to see the new value.(Apparently this was not the case.)
Another possibility is that the cancel() method is not called.
(Apparently this was not the case.)
Still one more possibility is that the cancel() method is being called on a different object instance. Try adding
System.out.println(System.identityHashCode(this))
to both the cancel() method and the while loop and see if they print the same value.按照 @mibollma 的建议,在已取消的实例变量上使用 volatile 。这将确保没有线程正在使用变量的缓存版本。如果这不起作用,您可以尝试以下建议:
您的
cancel()
方法可能没有被调用,带有 while 循环的线程不允许这样做。使用Thread.yield()
方法。在你的 while 循环中:
Use
volatile
on the cancelled instance variable as suggested by @mibollma. This will assure that no Thread is using a cached version of the variable. If that does not work, you can try the following suggestion:Your
cancel()
method is probably not being invoked, the Thread with the while loop is not permitting it. Use theThread.yield()
method.In your while loop: