【发布时间】:2012-08-20 09:06:25
【问题描述】:
我正在 Android 上进行开发,但我无法弄清楚为什么我的某些线程会进入“监控”状态。我读过它可能是因为“同步”问题,但我不确定一个对象如何不会释放他们的锁。
任何人都可以帮助解决这个问题,或者你看到我做错了什么吗?是同步对象没有被释放的问题,还是我的加载没有正确超时并锁定所有线程?
这是我使用同步的方式。
private Bitmap getFromSyncCache(String url) {
if (syncCache == null) return null;
synchronized (syncCache) {
if (syncCache.hasObject(url)) {
return syncCache.get(url);
} else {
return null;
}
}
}
这里:
bitmapLoader.setOnCompleteListener(new BitmapLoader.OnCompleteListener() {
@Override
public void onComplete(Bitmap bitmap) {
if (syncCache != null) {
synchronized (syncCache) {
syncCache.put(bitmapLoader.getLoadUrl(), bitmap);
}
}
if (asyncCache != null) addToAsyncCache(bitmapLoader.getLoadUrl(), bitmap);
if (onCompleteListener != null) onCompleteListener.onComplete(bitmap);
}
});
这是我的缓存
public class MemoryCache<T> implements Cache<T>{
private HashMap<String, SoftReference<T>> cache;
public MemoryCache() {
cache = new HashMap<String, SoftReference<T>>();
}
@Override
public T get(String id) {
if(!cache.containsKey(id)) return null;
SoftReference<T> ref = cache.get(id);
return ref.get();
}
@Override
public void put(String id, T object) {
cache.put(id, new SoftReference<T>(object));
}
@Override
public void clearCache() {
cache.clear();
}
@Override
public boolean hasObject(String id) {
return cache.containsKey(id);
}
这就是我从网络加载图像的方式:
private void threadedLoad(String url) {
cancel();
bytesLoaded = 0;
bytesTotal = 0;
try {
state = State.DOWNLOADING;
conn = (HttpURLConnection) new URL(url).openConnection();
bytesTotal = conn.getContentLength();
// if we don't have a total can't track the progress
if (bytesTotal > 0 && onProgressListener != null) {
// unused
} else {
conn.connect();
inStream = conn.getInputStream();
Bitmap bitmap = BitmapFactory.decodeStream(inStream);
state = State.COMPLETE;
if (state != State.CANCELED) {
if (bitmap != null) {
msgSendComplete(bitmap);
} else {
handleIOException(new IOException("Skia could not decode the bitmap and returned null. Url: " + loadUrl));
}
}
try {
inStream.close();
} catch(Exception e) {
}
}
} catch (IOException e) {
handleIOException(e);
}
}
【问题讨论】:
-
是否存在实际问题(例如死锁)或代码运行正常?
-
上面红色圈出的线程状态为“monitor”不是意味着死锁吗?
-
我的意思是你在运行应用程序时实际上遇到了死锁(没有调试)?
-
是(填空1234567890)
-
这是什么?
state = State.COMPLETE; if (state != State.CANCELED) {
标签: java android multithreading synchronization threadpool